Basic Programs
console.log, let, a bit of math. Week-one JS. If this feels slow, skip ahead.
10 programs · open one, then the next
Start with Hello World- 01Hello WorldBeginnerOne console.log. If this prints, Node or the browser console is actually working.Open example →js// The simplest JavaScript program console.log("Hello World");
- 02Display Your NameBeginnerlet name = "...". console.log can take extra arguments. They get spaces between them.Open example →js// Using a variable to store and display name let name = "John Doe"; console.log("My name is:", name);
- 03User InputBeginnerprompt() in the browser. Node has no prompt unless you install something.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. Coerce the strings or you glue 10 and 20 into 1020.Open example →js// Adding two numbers let num1 = 10; let num2 = 20; let sum = num1 + num2;
- 05Swap Two NumbersBeginnerTemp variable, then destructuring. The second one is the one you keep.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 TypesBeginnertypeof null is "object". That is a bug from 1995. Live with it.Open example →js// Primitive Data Types console.log("=== Primitive Types ==="); // Number
- 07Arithmetic OperationsBeginner+ - * / % **. / is float. % is remainder. ** is power. ^ is XOR.Open example →jslet a = 10; let b = 3; // Addition
- 08Convert Fahrenheit to CelsiusBeginnerC = (F - 32) * 5/9. 32°F should be 0. If you get 17.7 you forgot the minus 32.Open example →js// Formula: Celsius = (Fahrenheit - 32) * 5/9 let fahrenheit = 98.6; let celsius = (fahrenheit - 32) * 5 / 9;
- 09Calculate Simple InterestBeginnerP*R*T / 100. Rate 5 means 5%, not 0.05.Open example →js// Formula: SI = (P × R × T) / 100 // Where: P = Principal, R = Rate, T = Time let principal = 10000;
- 10Area and Perimeter of RectangleBeginnerarea = l*w. perimeter = 2*(l+w). Height is not a diagonal.Open example →js// Formula: Area = length × width // Formula: Perimeter = 2 × (length + width) let length = 10;