Conditional statements in JavaScript allow developers to control the flow of code by executing different blocks of code based on specific conditions. The main types of conditional statements are if
, else
, else if
, and switch
. This article explains each type with examples.
The if
statement executes a block of code if the specified condition evaluates to true
. This is the most basic conditional statement.
Example: Using an if
statement
let age = 18; if (age >= 18) { console.log("You are eligible to vote."); } // Output: You are eligible to vote.
The if-else
statement allows code to be executed if the condition is true
, and a different block of code if the condition is false
.
Example: Using an if-else
statement
let age = 16; if (age >= 18) { console.log("You are eligible to vote."); } else { console.log("You are not eligible to vote."); } // Output: You are not eligible to vote.
The if-else if-else
statement allows for multiple conditions to be checked sequentially. It executes the first block of code where the condition is true
. If none of the conditions are met, the else
block is executed.
Example: Using an if-else if-else
statement
let 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 { console.log("Grade: F"); } // Output: Grade: B
if
statements can be nested within each other to create more complex conditions. This is helpful when you have multiple layers of conditions.
Example: Using nested if
statements
let age = 20; let hasID = true; if (age >= 18) { if (hasID) { console.log("You are allowed to enter."); } else { console.log("ID required to enter."); } } else { console.log("You are too young to enter."); } // Output: You are allowed to enter.
The switch
statement is useful when you need to compare a single expression with multiple potential values. Each case
represents a value, and default
is executed if no cases match.
Example: Using a switch
statement
let day = 3; let dayName; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; case 4: dayName = "Thursday"; break; case 5: dayName = "Friday"; break; default: dayName = "Invalid day"; } console.log(dayName); // Output: Wednesday
The switch
statement can be more readable than multiple if-else
statements when checking a single expression against several values.
The ternary operator (condition ? expressionIfTrue : expressionIfFalse
) is a shorthand for simple if-else
statements. It returns one of two values based on the condition.
Example: Using the ternary operator
let age = 18; let accessMessage = age >= 18 ? "Access granted" : "Access denied"; console.log(accessMessage); // Output: Access granted
The ternary operator is a concise way to write conditional statements, especially for assignments or simple logic.
true
.true
, another if false
.if
statements within each other for complex conditions.if-else
statements.Conditional statements are essential for controlling the flow of a program. They allow developers to create logic that responds to different conditions and inputs, making applications interactive and dynamic. Understanding the various conditional statements in JavaScript helps in writing efficient and readable code.