Bird
Raised Fist0
Javascriptprogramming~5 mins

Ternary operator in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the ternary operator in JavaScript?
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.
Click to reveal answer
What does the ternary operator syntax look like?
Acondition ? valueIfTrue : valueIfFalse
Bif (condition) { valueIfTrue } else { valueIfFalse }
Ccondition : valueIfTrue ? valueIfFalse
DvalueIfTrue ? condition : valueIfFalse
What will this code return?
let x = 5; let result = (x > 3) ? 'Yes' : 'No';
A'No'
B5
Cundefined
D'Yes'
Can ternary operators replace all if-else statements?
AYes, always
BNo, only simple conditions
COnly in loops
DOnly with numbers
What happens if the condition in a ternary operator is false?
AThe value after the colon is returned
BThe value after the question mark is returned
CAn error occurs
DThe condition is re-evaluated
Is this a valid ternary operator?
let a = 10; let b = a > 5 ? 'big' : 'small';
ANo
BOnly if a is a string
CYes
DOnly if a is less than 5
Explain how the ternary operator works and give a simple example.
Think of it as a short if-else that picks one of two values.
You got /4 concepts.
    Describe when you should use the ternary operator instead of if-else statements.
    Use it for quick decisions, not for complicated choices.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the ternary operator condition ? expr1 : expr2 do in JavaScript?
      easy
      A. Executes both expr1 and expr2 and returns their sum.
      B. Always returns expr2 regardless of the condition.
      C. Throws an error if condition is false.
      D. Returns expr1 if condition is true, otherwise returns expr2.

      Solution

      1. Step 1: Understand the ternary operator structure

        The ternary operator has the form condition ? expr1 : expr2. It checks the condition.
      2. Step 2: Determine the returned value based on the condition

        If the condition is true, it returns expr1. Otherwise, it returns expr2.
      3. Final Answer:

        Returns expr1 if condition is true, else expr2 -> Option D
      4. Quick Check:

        condition ? expr1 : expr2 = expr1 or expr2 [OK]
      Hint: Remember: condition ? trueValue : falseValue [OK]
      Common Mistakes:
      • Thinking both expressions run always
      • Confusing the order of expr1 and expr2
      • Assuming it throws error on false condition
      2. Which of the following is the correct syntax for using the ternary operator in JavaScript?
      easy
      A. condition ? expr1 : expr2;
      B. condition ? expr1, expr2;
      C. condition : expr1 ? expr2;
      D. condition ? (expr1 : expr2);

      Solution

      1. Step 1: Recall the ternary operator syntax

        The correct syntax is condition ? expr1 : expr2 with a question mark and colon separating parts.
      2. Step 2: Check each option for correct punctuation

        condition ? expr1 : expr2; matches the correct syntax. Options B, C, and D misuse commas, colons, or parentheses.
      3. Final Answer:

        condition ? expr1 : expr2; -> Option A
      4. Quick Check:

        Syntax is condition ? expr1 : expr2 [OK]
      Hint: Look for ? and : in correct order with no extra symbols [OK]
      Common Mistakes:
      • Using commas instead of colon
      • Swapping ? and :
      • Adding unnecessary parentheses
      3. What is the output of this code?
      const age = 20;
      const canVote = age >= 18 ? 'Yes' : 'No';
      console.log(canVote);
      medium
      A. 'No'
      B. 'Yes'
      C. true
      D. undefined

      Solution

      1. Step 1: Evaluate the condition age >= 18

        Since age is 20, which is greater than or equal to 18, the condition is true.
      2. Step 2: Determine the value assigned to canVote

        Because the condition is true, canVote is assigned the string 'Yes'.
      3. Final Answer:

        'Yes' -> Option B
      4. Quick Check:

        20 >= 18 ? 'Yes' : 'No' = 'Yes' [OK]
      Hint: Check condition result true or false to pick value [OK]
      Common Mistakes:
      • Confusing true and false branches
      • Thinking it returns boolean true/false
      • Ignoring the condition value
      4. Find the error in this code snippet:
      let score = 85;
      let grade = score > 90 ? 'A' : score > 80 ? 'B' : 'C';
      console.log(grade);
      medium
      A. The code is correct and will output 'B'.
      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

      1. Step 1: Analyze the nested ternary expression

        The expression uses nested ternary operators without parentheses, which is allowed in JavaScript.
      2. 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'.
      3. Final Answer:

        The code is correct and will output 'B'. -> Option A
      4. 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

      1. Step 1: Understand the conditions for status

        score <= 0 means 'Fail', between 1 and 50 means 'Pass', and above 50 means 'Excellent'.
      2. 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.
      3. Final Answer:

        const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent'; -> Option C
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard