Conditional Statements
Conditional statements in JavaScript allow you to make decisions in your code by executing different blocks of code based on certain conditions. This is essential for adding logic and making your programs dynamic.
if...else Statement
The if...else statement is used to run a block of code if a specified condition is true, and optionally run another block if the condition is false.
Syntax
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
// Output: "You are eligible to vote."
if...else if...else Statement
The if...else if...else statement allows you to test multiple conditions.
Syntax
if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition2 is true
} else {
// code to execute if none of the above conditions are true
}
Example
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"
switch Statement
The switch statement is used to evaluate a value against multiple possible cases. It is a cleaner alternative to using multiple else if statements when you have many possible conditions to check.
Syntax
switch (expression) {
case value1:
// code to execute if expression === value1
break;
case value2:
// code to execute if expression === value2
break;
default:
// code to execute if none of the cases match
}
Example
let day = 3;
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");
}
// Output: "Wednesday"
The break statement is used to terminate the case once it has been executed. If you omit break, JavaScript will continue executing subsequent cases, a behavior known as fall-through.
Ternary Operator
The ternary operator is a shorthand way of writing an if...else statement. It is particularly useful for simple conditions.
Syntax
condition ? expressionIfTrue : expressionIfFalse;
Example
let age = 16;
let eligibility = age >= 18 ? "Eligible to vote" : "Not eligible to vote";
console.log(eligibility);
// Output: "Not eligible to vote"
Logical Operators
Logical operators are used to combine multiple conditions in a single statement.
&&(Logical AND): True if both operands are true.||(Logical OR): True if at least one operand is true.!(Logical NOT): Negates a boolean value.
Example using Logical AND
let age = 25;
let hasID = true;
if (age >= 18 && hasID) {
console.log("You are allowed to enter.");
} else {
console.log("You are not allowed to enter.");
}
// Output: "You are allowed to enter."
Example using Logical OR
let isWeekend = false;
let isHoliday = true;
if (isWeekend || isHoliday) {
console.log("You can relax today.");
} else {
console.log("You need to work today.");
}
// Output: "You can relax today."
Example using Logical NOT
let isRaining = false;
if (!isRaining) {
console.log("It's not raining, let's go for a walk.");
}
// Output: "It's not raining, let's go for a walk."
Nested Conditional Statements
You can also nest conditional statements, meaning you place one if or else if inside another if or else.
Example
let age = 20;
let hasLicense = true;
if (age >= 18) {
if (hasLicense) {
console.log("You can drive.");
} else {
console.log("You need a license to drive.");
}
} else {
console.log("You are not old enough to drive.");
}
// Output: "You can drive."
Best Practices for Conditionals
-
Use the Ternary Operator for Simple Conditions: When you have a simple
if...elsethat assigns a value, consider using the ternary operator to simplify your code. -
switchfor Multiple Conditions: Use aswitchstatement when you have a lot ofelse ifconditions that compare a single variable against multiple values. -
Logical Operators for Conciseness: Combine multiple conditions with logical operators to avoid nested or repetitive
ifstatements, making your code cleaner.
Summary
In this section, you’ve learned about different types of conditional statements, including if...else, switch, and the ternary operator. You also explored the use of logical operators to combine conditions effectively. Conditionals are essential in making decisions in your code, helping you add logic and control flow to your programs.