JavaScript if...else statement
The if...else
statement in JavaScript is a control structure that allows you to execute one block of code if a condition evaluates to true
and another block of code if the condition evaluates to false
. This provides a way to handle binary decisions and control the flow of your program based on different conditions.
Syntax
The basic syntax of the if...else
statement is:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
How It Works
- Condition Evaluation: The
condition
inside theif
parentheses is evaluated. It should be an expression that returns a boolean value (true
orfalse
). - Code Execution:
- If the condition evaluates to
true
, the block of code following theif
statement is executed. - If the condition evaluates to
false
, the block of code following theelse
statement is executed.
- If the condition evaluates to
Example
let age = 20;
if (age >= 18) {
console.log('You are an adult.');
} else {
console.log('You are a minor.');
}
- Explanation:
- The
if
condition checks ifage
is greater than or equal to 18. - If
age
is 20, which is greater than 18, the condition is true, and'You are an adult.'
is logged. - If
age
were less than 18, the condition would be false, and'You are a minor.'
would be logged instead.
- The
Key Points
Single
else
Block: Theelse
block is optional. If you only need to execute code for thetrue
case and don’t need to handle thefalse
case, you can use just theif
statement.let number = 5; if (number > 0) { console.log('Number is positive'); }
Multiple Conditions: For handling more than two conditions, you can use
if...else if...else
statements.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: D'); }
- Explanation:
- This example categorizes the
score
into different grades based on multiple conditions.
- This example categorizes the
- Explanation:
Short-Circuiting: The
else
block only executes if none of the precedingif
orelse if
conditions are true.let number = -5; if (number > 0) { console.log('Number is positive'); } else { console.log('Number is zero or negative'); }
- Explanation:
- If
number
is not greater than 0, theelse
block is executed.
- If
- Explanation:
Combining with Logical Operators: You can use logical operators (
&&
,||
,!
) to combine multiple conditions in anif
statement.let temperature = 30; let isRaining = false; if (temperature > 25 && !isRaining) { console.log('It is hot and dry outside.'); } else { console.log('Conditions are not ideal.'); }
- Explanation:
- The
if
condition checks if the temperature is above 25 and it is not raining. If both conditions are true, the corresponding message is logged.
- The
- Explanation:
Summary
- Basic Usage:
if...else
allows you to execute code blocks based on whether a condition is true or false. - Single
else
: Theelse
block handles the case where theif
condition is false. - Multiple Conditions: Use
else if
to handle multiple conditions. - Logical Operators: Combine conditions using logical operators to create more complex decision-making.