JavaScript Basic Programs
Start here with simple JavaScript programs. Print text, add numbers, swap values and convert units. Each page explains the code line by line and shows the output.
10 programs · open one, then the next
Start with Hello World- 01Hello WorldBeginnerWrite a Hello World program in JavaScript with one console.log line. Simple explanation, how to run it in Node or the browser, and the exact output shown.Open example →js// The simplest JavaScript program console.log("Hello World");
- 02Display Your NameBeginnerDisplay your name in JavaScript using a let variable and console.log with two values. Clear explanation of let, const and var, plus the exact output.Open example →js// Using a variable to store and display name let name = "John Doe"; console.log("My name is:", name);
- 03User InputBeginnerRead user input in JavaScript with prompt() and print a greeting. Learn why prompt returns a string and why Node has no prompt, with explanation and output.Open example →js// Getting user input using prompt() let name = prompt("Enter your name:"); let age = prompt("Enter your age:");
- 04Sum of Two NumbersBeginnerAdd two numbers in JavaScript and print the sum. Learn why "10" + "20" gives 1020 and how Number() and parseInt() differ, with explanation and output.Open example →js// Adding two numbers let num1 = 10; let num2 = 20; let sum = num1 + num2;
- 05Swap Two NumbersBeginnerSwap two numbers in JavaScript three ways: a temp variable, array destructuring and arithmetic. Each method has an explanation and before and after output.Open example →js// Method 1: Using a temporary variable let a = 5; let b = 10; console.log("Before swap: a =", a, ", b =", b);
- 06JavaScript Data TypesBeginnerLearn JavaScript data types with typeof: number, string, boolean, null, undefined, symbol, bigint and objects. Clear explanation of each and the output.Open example →js// Primitive Data Types console.log("=== Primitive Types ==="); // Number
- 07Arithmetic OperationsBeginnerJavaScript arithmetic operators + - * / % ** and ++/--. Learn why ^ is not power and how to divide whole numbers, with a simple explanation and output.Open example →jslet a = 10; let b = 3; // Addition
- 08Convert Fahrenheit to CelsiusBeginnerConvert Fahrenheit to Celsius in JavaScript with C = (F - 32) * 5 / 9. Includes a reusable function and toFixed rounding, with explanation and output.Open example →js// Formula: Celsius = (Fahrenheit - 32) * 5/9 let fahrenheit = 98.6; let celsius = (fahrenheit - 32) * 5 / 9;
- 09Calculate Simple InterestBeginnerCalculate simple interest in JavaScript with SI = P × R × T / 100. Learn why rate 5 means 5 percent and return an object, with explanation and output.Open example →js// Formula: SI = (P × R × T) / 100 // Where: P = Principal, R = Rate, T = Time let principal = 10000;
- 10Area and Perimeter of RectangleBeginnerFind the area and perimeter of a rectangle in JavaScript. Learn length × width and 2 × (length + width) using functions, with explanation and output.Open example →js// Formula: Area = length × width // Formula: Perimeter = 2 × (length + width) let length = 10;