If/Else Statements
The if statement lets your program make decisions. If a condition
is true, a block of code runs. Unlike Python which uses indentation to define
blocks, JavaScript uses curly braces { }.
const age = 18;
// Simple if
if (age >= 18) {
console.log("You are an adult.");
}
// if...else
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You cannot vote yet.");
}
// if...else if...else
const score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else if (score >= 60) {
console.log("Grade: D");
} else {
console.log("Grade: F");
}
// Output: "Grade: B"
Logical Operators in Conditions
const age = 25;
const hasLicense = true;
const isSuspended = false;
// AND (&&) — both conditions must be true
if (age >= 16 && hasLicense) {
console.log("You can drive.");
}
// OR (||) — at least one condition must be true
if (age < 13 || age > 65) {
console.log("Discounted ticket.");
}
// NOT (!) — inverts a boolean
if (!isSuspended) {
console.log("Account is active.");
}
// Combining operators
if (age >= 16 && hasLicense && !isSuspended) {
console.log("You are cleared to drive.");
}
Switch Statements
Use switch when you need to compare a single value against many
possible options. It is cleaner than a long chain of if...else if
statements.
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the work week.");
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
console.log("Midweek.");
break;
case "Friday":
console.log("Almost the weekend!");
break;
case "Saturday":
case "Sunday":
console.log("Weekend!");
break;
default:
console.log("Not a valid day.");
}
// Output: "Start of the work week."
Without break, JavaScript "falls through" to the next case
and keeps executing. This is intentional for grouping cases (like
Tuesday/Wednesday/Thursday above), but forgetting break is a
common source of bugs.
// Practical example: HTTP status codes
const status = 404;
switch (status) {
case 200:
console.log("OK — request succeeded.");
break;
case 301:
console.log("Moved Permanently.");
break;
case 404:
console.log("Not Found.");
break;
case 500:
console.log("Internal Server Error.");
break;
default:
console.log(`Unknown status: ${status}`);
}
// Output: "Not Found."
Ternary Operator
The ternary operator is a shorthand for simple if...else
statements. It fits on one line and is useful for assigning values
conditionally:
// Syntax: condition ? valueIfTrue : valueIfFalse
const age = 20;
const status = age >= 18 ? "adult" : "minor";
console.log(status); // "adult"
// Equivalent if...else:
// let status;
// if (age >= 18) {
// status = "adult";
// } else {
// status = "minor";
// }
// Common uses
const score = 85;
const grade = score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : "F";
const items = 3;
console.log(`You have ${items} item${items === 1 ? "" : "s"}.`);
// "You have 3 items."
// In template literals
const loggedIn = true;
console.log(`Welcome${loggedIn ? ", user" : ""}!`);
// "Welcome, user!"
For Loops
The classic for loop repeats a block of code a specific number
of times. It has three parts: initialization, condition, and update.
// Basic for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// 0, 1, 2, 3, 4
// Counting by twos
for (let i = 0; i <= 10; i += 2) {
console.log(i);
}
// 0, 2, 4, 6, 8, 10
// Counting backwards
for (let i = 5; i > 0; i--) {
console.log(i);
}
// 5, 4, 3, 2, 1
// Looping through an array by index
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(`${i}: ${fruits[i]}`);
}
// "0: apple"
// "1: banana"
// "2: cherry"
Practical Example: Building a Multiplication Table
const number = 7;
for (let i = 1; i <= 10; i++) {
console.log(`${number} x ${i} = ${number * i}`);
}
// 7 x 1 = 7
// 7 x 2 = 14
// ... up to 7 x 10 = 70
While and Do-While Loops
while Loop
A while loop repeats as long as its condition is true. Use it
when you don't know in advance how many times you need to loop.
// Count up to a threshold
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
// 0, 1, 2, 3, 4
// Practical: halving a number until it's below 1
let value = 100;
let steps = 0;
while (value >= 1) {
value = value / 2;
steps++;
}
console.log(`Took ${steps} steps to go below 1.`);
// "Took 7 steps to go below 1."
If the condition never becomes false, your program will hang. Always make sure something inside the loop is moving toward the exit condition.
do...while Loop
A do...while loop always runs at least once, then checks the
condition before repeating:
// Runs at least once even if condition is false
let input;
do {
input = prompt("Enter 'quit' to exit:");
console.log(`You entered: ${input}`);
} while (input !== "quit");
// Practical: generating a random number until we get one above 0.9
let random;
let attempts = 0;
do {
random = Math.random();
attempts++;
} while (random < 0.9);
console.log(`Got ${random.toFixed(4)} after ${attempts} attempts.`);
for...of and for...in
for...of iterates over values and works
with arrays, strings, Maps, Sets, and other iterables. for...in
iterates over keys (property names) and is designed for
objects. Using for...in on arrays is a common mistake — it
iterates over index strings and can include inherited properties.
for...of — Iterate Over Values
// Arrays — get each value directly
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
// "apple", "banana", "cherry"
// Strings — iterate over characters
const word = "Hello";
for (const char of word) {
console.log(char);
}
// "H", "e", "l", "l", "o"
// With array destructuring
const entries = [["name", "Alice"], ["age", 25]];
for (const [key, value] of entries) {
console.log(`${key}: ${value}`);
}
// "name: Alice", "age: 25"
for...in — Iterate Over Object Keys
// Objects — get each property name
const user = {
name: "Alice",
age: 25,
city: "London"
};
for (const key in user) {
console.log(`${key}: ${user[key]}`);
}
// "name: Alice"
// "age: 25"
// "city: London"
// Why NOT to use for...in with arrays:
const colors = ["red", "green", "blue"];
for (const index in colors) {
console.log(typeof index); // "string" — not a number!
console.log(index); // "0", "1", "2" — string keys
}
// Use for...of instead for arrays
Break and Continue
break exits a loop entirely. continue skips the
current iteration and moves to the next one.
break — Exit Early
// Find the first negative number
const numbers = [3, 7, -2, 8, -5, 1];
let firstNegative = null;
for (const num of numbers) {
if (num < 0) {
firstNegative = num;
break; // stop searching
}
}
console.log(firstNegative); // -2
// Search for a user
const users = ["Alice", "Bob", "Charlie", "Diana"];
for (let i = 0; i < users.length; i++) {
if (users[i] === "Charlie") {
console.log(`Found Charlie at index ${i}`);
break;
}
}
continue — Skip This Iteration
// Print only even numbers
for (let i = 0; i < 10; i++) {
if (i % 2 !== 0) {
continue; // skip odd numbers
}
console.log(i);
}
// 0, 2, 4, 6, 8
// Skip blank entries
const data = ["Alice", "", "Bob", "", "", "Charlie"];
for (const name of data) {
if (name === "") {
continue; // skip empty strings
}
console.log(`Processing: ${name}`);
}
// "Processing: Alice"
// "Processing: Bob"
// "Processing: Charlie"
Labeled Statements (Advanced)
// Break out of nested loops using labels
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break outer; // breaks the outer loop, not just inner
}
console.log(`i=${i}, j=${j}`);
}
}
// "i=0, j=0"
// "i=0, j=1"
// "i=0, j=2"
// "i=1, j=0"
// (stops here)
Summary
if/else if/else— make decisions based on conditionsswitch— compare one value against many options (don't forgetbreak!)condition ? a : b— ternary operator for inline conditionalsforloop — repeat a known number of timeswhile— repeat while a condition is truedo...while— always runs at least oncefor...of— iterate over values (arrays, strings)for...in— iterate over keys (objects)break— exit a loop early;continue— skip to next iteration
You can now make decisions and repeat actions in JavaScript. Next up: functions — reusable blocks of code that make your programs modular and organized.