JavaScript if-else Statements
In JavaScript, conditional statements are used to control the flow of the program. The most common conditional statements are if
, else
, and else if
.
These statements help you execute certain blocks of code based on whether a condition evaluates to true or false.
Types of if-else Statements
- if statement
- if-else statement
- else-if statement
1. if
Statement
You use the if
statement to execute a block of code if a given condition evaluates to true.
if (condition) {// code to be executed if condition is true}
Example: Simple if
Statement
if (5 > 3) {alert("5 is greater than 3");}if (2 < 1) {alert("2 is less than 1");}
In this example, the first if
condition 5 > 3
evaluates to true
, so the alert box "5 is greater than 3" will show up. The second condition 2 < 1
is false
, so no alert will appear.
Using Variables in if
Conditions
var age = 25;var minimumAge = 18;if (age >= minimumAge) {alert("You are eligible to vote!");}
This example compares two variables to check if someone is old enough to vote. If the condition evaluates to true, an alert message will be shown.
Curly Braces
If the if
block contains only one statement, you can skip the curly braces {}.
if (10 === 10) alert("10 equals 10");
Comparison Operators
Be careful when using comparison operators like ==
and ===
. The ==
operator checks only for equality in value, while ===
checks both value and type.
if (2 == "2") { alert("== operator does not consider types of operands"); }
if (2 === "2") { alert("=== operator considers types of operands"); }
2. else
Statement
The else
statement is used when you want to execute a block of code when the if
condition evaluates to false.
if (condition) { // Execute this code if true } else { // Execute this code if false }
Example: Simple if-else
Statement
var temperature = 15;
if (temperature > 20) { alert("It's warm outside"); } else { alert("It's cold outside"); }
3. else if
Statement
You can use else if
to test multiple conditions after an if
statement. If the if
condition is false, it checks the else if
condition.
if (condition) { // Execute this code if true } else if (condition) { // Execute this code if this second condition is true }
Example: Using else if
var day = "Monday";
if (day === "Friday") { alert("It's almost the weekend!"); } else if (day === "Monday") { alert("The week just started!"); } else { alert("It's a regular day."); }
Multiple else if
Statements
You can use multiple else if
statements to check several conditions one after the other.
var score = 85;
if (score >= 90) { alert("You got an A"); } else if (score >= 80) { alert("You got a B"); } else if (score >= 70) { alert("You got a C"); } else { alert("You failed"); }
Points to Remember
- Use
if-else
statements to control the flow of the program based on conditions. - There are three main types:
if
condition, if-else
condition, and else-if
condition. - The
if
condition must have a conditional expression inside parentheses ()
, followed by either a single statement or a code block wrapped in curly braces {}
. - The
else if
statement must come after an if
condition, and can be used multiple times. - There must be only one
else
statement at the end, and it comes after all if
or else if
statements.
Note: The else
statement is optional. If it's not provided, no code will be executed when the condition is false.