Recall & Review
beginner
What is the purpose of an if–else statement in JavaScript?
An if–else statement lets the program choose between two paths: one if a condition is true, and another if it is false.
Click to reveal answer
beginner
Write the basic syntax of an if–else statement in JavaScript.
if (condition) {<br> // code runs if condition is true<br>} else {<br> // code runs if condition is false<br>}
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
The code inside the if block is skipped, and the program continues after the if statement without running any alternative code.
Click to reveal answer
intermediate
Can you use multiple else if blocks in JavaScript? What is their purpose?
Yes, multiple else if blocks let you check several conditions in order, running the code for the first true condition.Click to reveal answer
beginner
Explain with an example how if–else helps in decision making in code.
Example:<br>if (temperature > 30) {<br> console.log('It is hot');<br>} else {<br> console.log('It is not hot');<br>}<br>This helps the program decide what message to show based on temperature.
Click to reveal answer
What will the following code print?<br>if (5 > 10) { console.log('Yes'); } else { console.log('No'); }
✗ Incorrect
Since 5 is not greater than 10, the else block runs and prints 'No'.
Which keyword is used to check an additional condition after an if statement?
✗ Incorrect
JavaScript uses 'else if' (two words) to check another condition after if.
What happens if the condition in an if statement is true?
✗ Incorrect
When the condition is true, the code inside the if block runs.
Can an if statement exist without an else block?
✗ Incorrect
The else block is optional; you can have just an if statement.
What is the output of this code?<br>if (false) { console.log('A'); } else if (true) { console.log('B'); } else { console.log('C'); }
✗ Incorrect
The first condition is false, the else if condition is true, so it prints 'B'.
Describe how an if–else statement controls the flow of a program.
Think about how the program chooses what code to run based on a question.
You got /4 concepts.
Write a simple if–else statement in JavaScript that checks if a number is positive or not.
Use console.log to show messages for both cases.
You got /4 concepts.