JavaScript Basics Examples

Beginner-friendly JavaScript programs covering variables, arithmetic, swapping values, random numbers, and everyday unit conversions. These concise examples build confidence and show how JS evaluates expressions and handles simple logic.

JavaScript Program to Pass a Function as Parameter

Pass functions around.

Output


                      

JavaScript Program To Print Hello World

Print a message to the console.

Output


                      

JavaScript Program to Add Two Numbers

Add two numbers and print the sum.

Output


                      

JavaScript Program to Calculate the Area of a Triangle

Use 1/2 * base * height.

Output


                      

JavaScript Program to Swap Two Variables

Swap using destructuring.

Output


                      

JavaScript Program to Solve Quadratic Equation

Compute roots using the quadratic formula.

Output


                      

JavaScript Program to Convert Kilometers to Miles

Convert km to miles (1 km ≈ 0.621371 mi).

Output


                      

Javascript Program to Convert Celsius to Fahrenheit

Use (C*9/5)+32.

Output


                      

Javascript Program to Generate a Random Number

Random integer in a range.

Output


                      

Javascript Program to Check if a number is Positive, Negative, or Zero

Check sign of a number.

Output


                      

Javascript Program to Check if a Number is Odd or Even

Use modulo % 2.

Output


                      

JavaScript Program to Find the Largest Among Three Numbers

Find max among three numbers.

Output


                      

JavaScript Program to Make a Simple Calculator

Perform +, -, *, /.

Output


                      

JavaScript Program to Check if the Numbers Have Same Last Digit

Compare last digit.

Output


                      

JavaScript Program to Find the Factors of a Number

List all divisors.

Output


                      

JavaScript Program to Guess a Random Number

Simple guess demo.

Output


                      

JavaScript Program to Shuffle Deck of Cards

Fisher–Yates shuffle.

Output


                      

JavaScript Program to Convert Decimal to Binary

Use toString(2).

Output


                      

JavaScript Program to Find ASCII Value of Character

Use charCodeAt.

Output


                      

JavaScript Program to Sort Words in Alphabetical Order

Sort words.

Output


                      

JavaScript Program to Check the Number of Occurrences of a Character in the String

Count char occurrences.

Output


                      

JavaScript Program to Check Whether a String Starts and Ends With Certain Characters

Use startsWith/endsWith.

Output


                      

JavaScript Program to Format Numbers as Currency Strings

Use toLocaleString.

Output


                      

JavaScript Program to Check if a String Starts With Another String

Use startsWith.

Output


                      

JavaScript Program to Check Leap Year

Check leap year rules.

Output


                      

JavaScript Program to Add Element to Start of an Array

Use unshift.

Output


                      

JavaScript Program to Extract Given Property Values from Objects as Array

Map property values.

Output


                      

JavaScript Program To Check If A Variable Is undefined or null

Check variable state.

Output


                      

JavaScript Program to Set a Default Parameter Value For a Function

Default parameter.

Output


                      

JavaScript Program to Illustrate Different Set Operations

Union, intersection, difference.

Output


                      

Javascript Program to Generate a Random Number Between Two Numbers

Random in [min,max].

Output


                      

JavaScript Program To Get The Current URL

Get current URL.

Output


                      

JavaScript Program to Check If a Variable is of Function Type

Use typeof.

Output


                      

JavaScript Program To Work With Constants

Use const.

Output


                      

JavaScript Program to Generate a Range of Numbers and Characters

Generate ranges.

Output


                      

JavaScript Program to Perform Function Overloading

Handle different args.

Output


                      

JavaScript Program to Implement a Stack

Stack with push/pop.

Output


                      

JavaScript Program to Implement a Queue

Queue with enqueue/dequeue.

Output


                      

JavaScript Program to Check if a Number is Float or Integer

Use Number.isInteger.

Output


                      

JavaScript Program to Get the Dimensions of an Image

Read image width/height.

Output


                      

JavaScript Program to Remove All Whitespaces From a Text

Remove all whitespace.

Output


                      

JavaScript Program to Write to Console

Use console methods.

Output


                      

Keep Practicing

Use the online compiler to run examples and test variations. Reinforce learning by building small scripts for each topic.

FAQ

Yes. You can copy the code into the JavaScript compiler page or your browser console to see the output. Some examples use console.log to print results.

Most examples use foundational syntax available in modern browsers. Where templates or arrow functions are used, they follow ES6 conventions widely supported today.

Absolutely. These examples are provided for learning. Copy, tweak values, and experiment to understand how the logic changes.

Start with Hello World, variables, and arithmetic. Then try control flow, functions, strings, and arrays to build confidence.

Learn JavaScript by Practicing Examples

Hands-on practice is the fastest way to understand JavaScript. Each example above focuses on a single concept—from strings, arrays, and objects to math utilities, dates, loops, and functions. Try editing variables, adding conditions, or refactoring logic, then observe the output changes instantly.

Use our tools to deepen your learning: JavaScript Compiler to run snippets, and the JavaScript Tutorial for guided theory and practice.

Beginner-Friendly

Start with variables, operators, and control flow. Build confidence with simple, readable programs.

Practical Patterns

Practice common tasks like formatting strings, manipulating arrays, and working with dates.

Grow Skills

Advance to objects, classes, and asynchronous patterns such as promises and async/await.