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?
ACheck multiple conditions inside each other
BRun code without any conditions
COnly check one condition at a time
DSkip all conditions
✗ Incorrect
Nested conditionals let you check one condition inside another, allowing multiple levels of decision-making.
In nested conditionals, which condition is checked first?
AThe outer condition
BBoth at the same time
CThe inner condition
DNone
✗ Incorrect
The outer condition is checked first. Only if it is true does JavaScript check the inner condition.
What will this code print?
let x = 10;
if (x > 5) {
if (x < 8) {
console.log('A');
} else {
console.log('B');
}
} else {
console.log('C');
}
ANothing
BA
CC
DB
✗ 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?
Awhile
BBoth C and D
Cswitch
Delse if
✗ Incorrect
Both else if chains and switch statements can sometimes replace nested conditionals for clearer code.
Is this a valid nested conditional?
if (a) {
if (b) {
// code
}
}
ANo
BOnly if a and b are numbers
CYes
DOnly if a and b are strings
✗ 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.
Practice
(1/5)
1. What does a nested conditional statement mean in JavaScript? if (condition1) { if (condition2) { /* code */ } }
easy
A. A loop inside a condition
B. A condition inside another condition
C. Two conditions checked at the same time
D. A function inside a condition
Solution
Step 1: Understand the structure of nested conditionals
Nested conditionals mean one if statement is placed inside another if statement's block.
Step 2: Identify the meaning of the code snippet
The code checks condition1 first, and only if true, it checks condition2 inside it.
2. Which of the following is the correct syntax for nested if statements in JavaScript?
easy
A. if (a > 0) { if (b > 0) { console.log('Both positive'); } }
B. if (a > 0) if (b > 0) console.log('Both positive'); }
C. if (a > 0) { if (b > 0) console.log('Both positive'); } else }
D. if a > 0 { if b > 0 { console.log('Both positive'); } }
Solution
Step 1: Check proper use of braces and parentheses
if (a > 0) { if (b > 0) { console.log('Both positive'); } } uses parentheses for conditions and braces to group code blocks correctly.
Step 2: Verify syntax correctness
The other options have missing or misplaced braces or parentheses causing syntax errors.
Final Answer:
if (a > 0) { if (b > 0) { console.log('Both positive'); } } -> Option A
Quick Check:
Correct nested if syntax = if (a > 0) { if (b > 0) { console.log('Both positive'); } } [OK]
Hint: Match each if with parentheses and braces [OK]
Common Mistakes:
Missing braces for inner if block
Omitting parentheses around conditions
Extra or misplaced braces
3. What will be the output of this code?
let x = 5; if (x > 0) { if (x < 10) { console.log('A'); } else { console.log('B'); } } else { console.log('C'); }
medium
A. No output
B. B
C. C
D. A
Solution
Step 1: Evaluate the outer if condition
x = 5, which is greater than 0, so the outer if block runs.
Step 2: Evaluate the inner if condition
Inside, x < 10 is true (5 < 10), so it prints 'A'.
Final Answer:
A -> Option D
Quick Check:
5 > 0 and 5 < 10 prints 'A' [OK]
Hint: Check outer condition first, then inner [OK]
Common Mistakes:
Ignoring inner else block
Confusing output with 'B' or 'C'
Assuming no output if conditions are true
4. Find the error in this nested conditional code:
let num = 10; if (num > 5) { if num < 15 { console.log('Inside range'); } }
medium
A. Using console.log incorrectly
B. Missing braces for outer if block
C. Missing parentheses around inner if condition
D. No error, code is correct
Solution
Step 1: Check syntax of inner if statement
Inner if is written as if num < 15 { missing parentheses around condition.
Step 2: Confirm correct syntax for if statements
JavaScript requires parentheses around conditions: if (num < 15) {.
Final Answer:
Missing parentheses around inner if condition -> Option C
Quick Check:
if conditions need parentheses [OK]
Hint: Always put parentheses around if conditions [OK]
Common Mistakes:
Forgetting parentheses in if conditions
Assuming braces are optional
Thinking console.log causes error
5. You want to write a nested conditional that prints: - 'Small positive' if a number is > 0 and < 10 - 'Large positive' if a number is >= 10 - 'Non-positive' otherwise Which code correctly implements this?
hard
A. if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); }
B. if (num > 0 && num < 10) { console.log('Small positive'); } else if (num >= 10) { console.log('Large positive'); } else { console.log('Non-positive'); }
C. if (num > 0) { if (num <= 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); }
D. if (num >= 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); }
Solution
Step 1: Check conditions for small positive numbers
if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } checks if num > 0, then inside if num < 10 to print 'Small positive'.
Step 2: Check conditions for large positive and non-positive
Else inside outer if prints 'Large positive' for num >= 10, else prints 'Non-positive'.
Step 3: Verify other options
The option with combined conditions (&& and else if) is logically correct but does not use nested if statements. The option with outer condition num >= 0 incorrectly classifies 0 as 'Small positive'. The option with inner condition num <= 10 incorrectly classifies 10 as 'Small positive'.
Final Answer:
if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } -> Option A
Quick Check:
Nested if with correct ranges = if (num > 0) { if (num < 10) { console.log('Small positive'); } else { console.log('Large positive'); } } else { console.log('Non-positive'); } [OK]
Hint: Check each condition range carefully in nested ifs [OK]
Common Mistakes:
Using <= instead of < for small positive
Including zero as positive
Not nesting conditions properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.