The ternary operator is a shortcut for an if-else statement. It uses the syntax: condition ? valueIfTrue : valueIfFalse.
Click to reveal answer
beginner
How do you write a ternary operator to check if a number is even or odd?
Example: <code>let result = (num % 2 === 0) ? 'Even' : 'Odd';</code> This sets result to 'Even' if num is even, otherwise 'Odd'.
Click to reveal answer
intermediate
Can the ternary operator be nested in JavaScript?
Yes, you can nest ternary operators inside each other, but it can make code hard to read. Example: <code>let result = score > 90 ? 'A' : score > 80 ? 'B' : 'C';</code>
Click to reveal answer
beginner
What is the return value of a ternary operator?
The ternary operator returns the value of either the expression after the question mark if true, or the expression after the colon if false.
Click to reveal answer
beginner
Why use the ternary operator instead of if-else?
It makes simple conditional assignments shorter and easier to read, especially when assigning values based on a condition.
B. Incorrect use of multiple ternary operators is not allowed.
C. Missing parentheses around nested ternary expressions.
D. The variable grade is not declared properly.
Solution
Step 1: Analyze the nested ternary expression
The expression uses nested ternary operators without parentheses, which is allowed in JavaScript.
Step 2: Evaluate the condition with score = 85
First condition score > 90 is false, so it checks score > 80, which is true, so grade is 'B'.
Final Answer:
The code is correct and will output 'B'. -> Option A
Quick Check:
Nested ternary without parentheses works [OK]
Hint: Nested ternary operators can be used without parentheses [OK]
Common Mistakes:
Assuming nested ternary needs parentheses
Thinking multiple ternaries are invalid syntax
Confusing variable declaration errors
5. You want to assign a variable status based on a number score: if score is 0 or less, status should be 'Fail'; if between 1 and 50, 'Pass'; otherwise 'Excellent'. Which ternary expression correctly does this?
hard
A. const status = score <= 0 ? 'Fail' : (score > 50 ? 'Pass' : 'Excellent');
B. const status = score < 0 ? 'Fail' : score < 50 ? 'Pass' : 'Excellent';
C. const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent';
D. const status = score === 0 ? 'Fail' : score < 50 ? 'Pass' : 'Excellent';
Solution
Step 1: Understand the conditions for status
score <= 0 means 'Fail', between 1 and 50 means 'Pass', and above 50 means 'Excellent'.
Step 2: Check each option against these conditions
const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent'; correctly checks score <= 0 for 'Fail', then score <= 50 for 'Pass', else 'Excellent'. Others have wrong ranges or conditions.
Final Answer:
const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent'; -> Option C
Quick Check:
Correct ranges and order in ternary [OK]
Hint: Check conditions cover all ranges without gaps [OK]
Common Mistakes:
Using wrong comparison operators
Missing equal signs causing wrong ranges
Incorrect order of conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.