0
0
Javascriptprogramming~20 mins

If statement in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if statements
What is the output of the following code?
Javascript
let x = 10;
if (x > 5) {
  if (x < 15) {
    console.log('A');
  } else {
    console.log('B');
  }
} else {
  console.log('C');
}
AB
BA
CC
DNo output
Attempts:
2 left
💡 Hint
Check the conditions step by step.
Predict Output
intermediate
2:00remaining
If statement with else if
What will be logged to the console?
Javascript
let score = 75;
if (score >= 90) {
  console.log('Excellent');
} else if (score >= 70) {
  console.log('Good');
} else {
  console.log('Try again');
}
AGood
BExcellent
CTry again
DNo output
Attempts:
2 left
💡 Hint
Check which condition matches the score first.
Predict Output
advanced
2:00remaining
If statement with logical operators
What is the output of this code?
Javascript
let a = 5;
let b = 10;
if (a > 3 && b < 15) {
  console.log('X');
} else if (a > 3 || b > 15) {
  console.log('Y');
} else {
  console.log('Z');
}
ANo output
BZ
CX
DY
Attempts:
2 left
💡 Hint
Both conditions in the first if must be true for 'X'.
Predict Output
advanced
2:00remaining
If statement with type coercion
What will be the output of this code?
Javascript
let val = '0';
if (val) {
  console.log('True');
} else {
  console.log('False');
}
ATrue
BFalse
CSyntaxError
DNo output
Attempts:
2 left
💡 Hint
Check how JavaScript treats non-empty strings in conditions.
Predict Output
expert
2:00remaining
If statement with variable scope
What is the value of x after running this code?
Javascript
let x = 1;
if (true) {
  let x = 2;
  if (true) {
    x = 3;
  }
}
console.log(x);
A2
BReferenceError
C3
D1
Attempts:
2 left
💡 Hint
Consider the scope of variables declared with let inside blocks.