Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind JavaScript else?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
block of code to be executed ___ the condition is true3Order
Put the learning moves in the order that makes the concept easiest to apply.
Use the else statement to specify a block of code to be executed if a condition is false .
The else if Statement
The else Statement
The else Statement
Use the else statement to specify a block of code to be executed if a condition is false .
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
}if (hour < 18) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}The else if Statement
Use the else if statement to specify a new condition if the first is false .
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
}if (time < 10) {
greeting = "Good morning";
}
else if (time < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}Example
let text;
if (Math.random() < 0.5) {
text = "<a href='https://example.com ExampleSite</a>"; } else { text = "<a href='https://wwf.org'>Visit WWF</a>"; } document.getElementById("demo").innerHTML = text;