JavaScript If Else Else If
JavaScript If Else Else If
w3schools.com LOG IN
Conditional Statements
Very often when you write code, you want to perform different actions for different
decisions.
https://www.w3schools.com/js/js_if_else.asp 1/9
1/16/2021 JavaScript if else else if
The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition
is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a
JavaScript error.
Example
Make a "Good day" greeting if the hour is less than 18:00:
Good day
Try it Yourself »
https://www.w3schools.com/js/js_if_else.asp 2/9
1/16/2021 JavaScript if else else if
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
Good day
https://www.w3schools.com/js/js_if_else.asp 3/9
1/16/2021 JavaScript if else else if
Try it Yourself »
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2
is true
} else {
// block of code to be executed if the condition1 is false and condition2
is false
}
Example
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than
20:00, create a "Good day" greeting, otherwise a "Good evening":
Good day
https://www.w3schools.com/js/js_if_else.asp 4/9