0
0
Javascriptprogramming~5 mins

Nested conditional statements in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 &gt;= 18) {
  if (age &lt;= 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
In nested conditionals, which condition is checked first?
AThe outer condition
BBoth at the same time
CThe inner condition
DNone
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');
}
ANothing
BA
CC
DB
Which keyword can sometimes replace nested conditionals for clarity?
Awhile
BBoth C and D
Cswitch
Delse if
Is this a valid nested conditional?<br>
if (a) {
  if (b) {
    // code
  }
}
ANo
BOnly if a and b are numbers
CYes
DOnly if a and b are strings
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.