Ternary Operator

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This Ternary Operator worksheet is part of the JavaScript Coding without HTML and CSS journey. Print it as a booklet?

Ternary Operator

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does the ternary operator use ? and : together?
A Yes
B No
2. Is the ternary operator a shorthand for if/else?
A No
B Yes
3. Does = replace ? in a correct ternary expression?
A Yes
B No
4. Can a ternary expression's result be stored in a variable?
A Yes
B No
5. Does the condition need a semicolon before the ?
A No
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1age >= 18 ? "adult" : "minor" AReturns "win" if score is over 50, otherwise "lose"
2score > 50 ? "win" : "lose" B:
3A shorthand way of writing an if/else statement CWrong - use :, not -, between the two values
4Separates the true and false results in a ternary Dcondition ? valueIfTrue : valueIfFalse
5let x = a = "yes" : "no"; EReturns "adult" if age is 18 or more, otherwise "minor"
6let y = a ? "yes" - "no"; FWrong - use ?, not =, after the condition

Definitions

?:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let status = age >= 18 _ "adult" : "minor";
2.
let result = score > 50 ? "win" _ "lose";
3.
let label = isReady _ "Go" : "Wait";
4.
let output = temp > 30 ? "hot" _ "cool";
5.
let grade = pass _ "Pass" : "Fail";

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let x = ok = "a" : "b";
B console.log(x);
C let y = 5;
Question 2
A let a = 5;
B let b = x > 3 - "hi" : "lo";
C console.log(b);
Question 3
A let n = 10;
B let z = 1;
C let lab = n==10 ? "a" ; "b";
D console.log(lab);
Question 4
A let age = 20;
B let t;
C console.log(t);
D t = age>=18 ? "a" "b";
Question 5
A let s = 75;
B let r;
C console.log(r);
D let ok2 = true;
E r = s>=50 && "p" : "f";

Answer Key

Ternary Operator
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
B — No
Q4
A — Yes
Q5
A — No
Section B: Match the Pairs
Q1
age >= 18 ? "adult" : "minor" → Returns "adult" if age is 18 or more, otherwise "minor"
Q2
score > 50 ? "win" : "lose" → Returns "win" if score is over 50, otherwise "lose"
Q3
A shorthand way of writing an if/else statement → condition ? valueIfTrue : valueIfFalse
Q4
Separates the true and false results in a ternary → :
Q5
let x = a = "yes" : "no"; → Wrong - use ?, not =, after the condition
Q6
let y = a ? "yes" - "no"; → Wrong - use :, not -, between the two values
Section C: Definitions
Q1
?: — A shorthand operator that returns one value if a condition is true and another if false, without needing if/else.
Section D: Fill in the Blanks
Q1
let status = age >= 18 ? "adult" : "minor";
Q2
let result = score > 50 ? "win" : "lose";
Q3
let label = isReady ? "Go" : "Wait";
Q4
let output = temp > 30 ? "hot" : "cool";
Q5
let grade = pass ? "Pass" : "Fail";
Section E: Spot the Error
Q1
A — Ternary condition uses ?, not =, before the values.
Q2
B — Ternary needs ?, not -, after the condition.
Q3
C — Ternary needs :, not ;, between the two values.
Q4
D — Missing the colon : between the two ternary values.
Q5
E — Ternary condition uses ?, not &&, before the values.