Understanding Control Structures in JavaScript
Control structures help you make decisions and control the flow of your program. They allow you to execute different parts of your code based on certain conditions. Here’s a look at the main control structures in JavaScript:
1. if
// Syntax:
if (condition) {
// Code to execute if condition is true
}
// Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
The if
statement lets you run a block of code if a specific condition is true. It’s used for basic decision-making.
2. else if
// Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
}
// Example:
let score = 85;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
}
The else if
statement follows an if
statement and lets you check multiple conditions. If the first if
condition is false, else if
allows you to check another condition.
3. else
// Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
// Example:
let temperature = 30;
if (temperature > 25) {
console.log("It's hot outside.");
} else {
console.log("It's cool outside.");
}
The else
statement is used when none of the previous conditions are true. It allows you to execute a block of code as a fallback.
4. switch
// Syntax:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if expression does not match any case
}
// Example:
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
The switch
statement is used to handle multiple possible values for a single variable. It is an alternative to multiple if-else
statements when you need to compare one value against many possible options.
5. case
// Syntax:
switch (expression) {
case value1:
// Code to execute
break;
}
// Example:
let fruit = "Apple";
switch (fruit) {
case "Apple":
console.log("It's an apple.");
break;
case "Banana":
console.log("It's a banana.");
break;
}
The case
keyword is used inside a switch
statement to define a block of code that will run if the switch
expression matches the case value.
6. default
// Syntax:
switch (expression) {
default:
// Code to execute if no case matches
}
// Example:
let color = "Red";
switch (color) {
case "Blue":
console.log("It's blue.");
break;
case "Green":
console.log("It's green.");
break;
default:
console.log("Unknown color");
}
The default
keyword is used in a switch
statement to define a block of code that will run if none of the case
values match the switch
expression.
In this example, if color is neither "Blue" nor "Green", the message "Unknown color" will be printed.
Summary
if
: Executes code based on whether a condition is true.else if
: Provides additional conditions to check if the initialif
condition is false.else
: Executes code when no previous conditions are true.switch
: Handles multiple conditions by comparing one value to various possible values.case
: Specifies a block of code to run if theswitch
expression matches a value.default
: Specifies a block of code to run if none of thecase
values match.
These control structures allow you to manage the flow of your program and handle different scenarios effectively.