JavaScript Basics Examples

Key takeaway: Practice JavaScript with 102+ runnable code examples — variables, arrays, strings, objects, math, dates, and DOM — all with expected output to verify instantly.

JavaScript is a programming language that makes web pages interactive. These examples are for front-end developers, bootcamp students, self-taught coders, and anyone preparing for JS interviews.

Last updated: August 2026 · 102 examples

Quick Start

  1. Pick an example from the sidebar
  2. Study the code and expected output
  3. Run it in our online JS compiler or browser console
  4. Modify the code to experiment

Who is this for: Front-end developers, bootcamp students, interview candidates, and self-taught coders building JavaScript skills. Use this when: you need quick reference code, want to practice for interviews, or learn JS concepts hands-on.

Related:JS Tutorial · JS Compiler · TypeScript Examples · Python Examples · MDN JavaScript Docs

JavaScript Program to Generate a Range of Numbers and Characters

Generate ranges.

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 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 Pass a Function as Parameter

Pass functions around.

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, run them instantly. Copy the code into our online JS compiler or your browser console (F12 → Console). Output uses console.log.

ES6+ (modern JavaScript). All examples work in Chrome, Firefox, Safari, and Edge. Arrow functions, template literals, and destructuring are used where appropriate.

Yes, absolutely. All examples are free to copy, modify, and share. Experimentation is the fastest way to learn JavaScript.

Start with basics, then build up. Begin with Hello World → variables → arithmetic → control flow → functions → strings → arrays → objects.

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.