Recall & Review
beginner
What is a nested conditional statement in JavaScript?
A nested conditional statement is an if or else statement placed inside another if or else block. It helps check multiple conditions step-by-step.
Click to reveal answer
beginner
Why use nested conditional statements?
Nested conditionals let you make decisions that depend on more than one condition. For example, checking if a person is a teenager and if they have a driver's license.Click to reveal answer
beginner
How does JavaScript decide which block to run in nested conditionals?
JavaScript checks the outer condition first. If it is true, it then checks the inner condition(s). It runs the code inside the first true condition it finds.
Click to reveal answer
beginner
Example: What will this code print?<br><pre>let age = 20;
if (age >= 18) {
if (age <= 25) {
console.log('Young adult');
} else {
console.log('Adult');
}
} else {
console.log('Minor');
}</pre>It will print
Young adult because age is 20, which is between 18 and 25, so the inner if condition is true.Click to reveal answer
intermediate
Can nested conditionals be replaced by other structures?
Yes, sometimes nested conditionals can be replaced by else if chains or switch statements for clearer code, but nesting is useful for checking conditions inside other conditions.
Click to reveal answer
What does a nested conditional statement allow you to do?
✗ Incorrect
Nested conditionals let you check one condition inside another, allowing multiple levels of decision-making.
In nested conditionals, which condition is checked first?
✗ Incorrect
The outer condition is checked first. Only if it is true does JavaScript check the inner condition.
What will this code print?<br>
let x = 10;
if (x > 5) {
if (x < 8) {
console.log('A');
} else {
console.log('B');
}
} else {
console.log('C');
}✗ Incorrect
x is 10, which is greater than 5 (outer condition true), but not less than 8 (inner condition false), so it prints 'B'.
Which keyword can sometimes replace nested conditionals for clarity?
✗ Incorrect
Both
else if chains and switch statements can sometimes replace nested conditionals for clearer code.Is this a valid nested conditional?<br>
if (a) {
if (b) {
// code
}
}✗ Incorrect
Yes, this is a valid nested conditional where one if statement is inside another.
Explain how nested conditional statements work in JavaScript and give a simple example.
Think about checking one condition inside another.
You got /4 concepts.
Describe a real-life situation where nested conditional statements would be useful.
Consider decisions that depend on more than one factor.
You got /3 concepts.