Challenge - 5 Problems
Unary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code using unary plus?
Consider the following JavaScript code. What will be printed to the console?
Javascript
let a = '5'; let b = +a; console.log(typeof b, b);
Attempts:
2 left
💡 Hint
Unary plus converts strings to numbers.
✗ Incorrect
The unary plus operator converts the string '5' to the number 5, so typeof b is 'number' and b is 5.
❓ Predict Output
intermediate2:00remaining
What is the output of this code using unary negation?
Look at this code snippet. What will it print?
Javascript
let x = 10; console.log(-x);
Attempts:
2 left
💡 Hint
Unary negation changes the sign of a number.
✗ Incorrect
The unary negation operator changes 10 to -10.
❓ Predict Output
advanced2:00remaining
What is the output of this code using prefix and postfix increment?
What will this code print to the console?
Javascript
let i = 3; console.log(i++); console.log(++i);
Attempts:
2 left
💡 Hint
Postfix returns the value then increments; prefix increments then returns.
✗ Incorrect
i++ prints 3 then increments to 4; ++i increments to 5 then prints 5.
❓ Predict Output
advanced2:00remaining
What is the output of this code using the typeof unary operator?
What will this code print?
Javascript
let val = null; console.log(typeof val);
Attempts:
2 left
💡 Hint
typeof null is a known JavaScript quirk.
✗ Incorrect
In JavaScript, typeof null returns 'object' due to legacy reasons.
❓ Predict Output
expert2:00remaining
What is the output of this code combining unary plus and logical NOT?
What will this code print to the console?
Javascript
let a = '0'; console.log(+!a);
Attempts:
2 left
💡 Hint
Logical NOT converts to boolean then negates; unary plus converts boolean to number.
✗ Incorrect
The string '0' is truthy, so !a evaluates to false. The unary plus converts false to the number 0.