0
0
Javascriptprogramming~20 mins

Ternary operator in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested ternary operator
What is the output of the following JavaScript code?
Javascript
const x = 5;
const y = 10;
const result = x > 3 ? (y < 15 ? 'A' : 'B') : 'C';
console.log(result);
A"B"
B"A"
C"C"
Dundefined
Attempts:
2 left
💡 Hint
Check the conditions step by step starting from the outer ternary.
Predict Output
intermediate
2:00remaining
Ternary operator with falsy values
What will be logged to the console?
Javascript
const value = 0;
const output = value ? 'Yes' : 'No';
console.log(output);
A"No"
Bundefined
C0
D"Yes"
Attempts:
2 left
💡 Hint
Remember how JavaScript treats 0 in boolean context.
Predict Output
advanced
2:00remaining
Ternary operator with side effects
What is the output of this code?
Javascript
let count = 0;
const result = count === 0 ? (count += 5) : (count += 10);
console.log(result, count);
A5 0
B10 10
C0 5
D5 5
Attempts:
2 left
💡 Hint
Check how the ternary operator changes the count variable.
Predict Output
advanced
2:00remaining
Ternary operator with different types
What will this code print?
Javascript
const a = true;
const b = false;
const output = a ? (b ? 1 : 'two') : 3;
console.log(typeof output, output);
A"string two"
B"number 1"
C"number 3"
D"boolean true"
Attempts:
2 left
💡 Hint
Evaluate the nested ternary carefully and check the type of the final value.
🧠 Conceptual
expert
2:00remaining
Understanding ternary operator evaluation order
Consider the code below. What is the value of variable result after execution?
Javascript
let x = 2;
const result = x > 1 ? x++ : --x;
A1
B3
C2
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that x++ returns the value before incrementing.