Understanding JavaScript Concepts

A beginner's guide to essential JavaScript concepts.

Control Structures

Control structures manage the flow of your program, including:

Examples:


let age = 18;
if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

for (let i = 0; i < 5; i++) {
    if (i === 2) break; // Exits the loop when i is 2
    console.log("Number:", i); // Outputs 0, 1
}

let day = 3;
switch (day) {
  case 1: 
    dayName = "Monday"; 
    break;
  case 2: 
    dayName = "Tuesday"; 
    break;
  case 3: 
    dayName = "Wednesday"; 
    break;
  default: 
    dayName = "Unknown day"; 
}