Ternary and Comma Operators
In short: condition ? ifTrue : ifFalse is the ternary operator —
a single expression that picks and returns one of two values, a compact
if/else you can drop straight into a variable or a template string. The
comma operator, a, b, c, evaluates each expression left to right and
yields only the last one — it's mostly seen tucked into a for loop's
update clause, squeezing two updates into the one slot the loop allows.

Pick a label with one line
The ternary operator replaces a small if/else with a single
expression — useful anywhere a value, not a statement, is expected:
age >= 18 is true, so the whole expression evaluates to "adult" — the
part after : is never touched. Written as if/else, this would need a
variable declared outside the block and assigned inside it; the ternary
does it in one line because it is an expression, not a statement.
Run two updates, keep only the last value
The comma operator lets several expressions share one spot, evaluating each in order and discarding every result except the final one:
This prints 8 2 6. All three expressions run, left to right: a++ bumps
a to 2, b++ bumps b to 6, and a + b (the last one) is 2 + 6 = 8, which becomes result's value — the earlier two results are thrown
away.
Advance two loop counters in one clause
A for loop's update clause has room for exactly one expression — the
comma operator is how you fit more than one increment there:
i++, j-- runs both updates every pass, in order, even though the loop
only "sees" one expression slot. Nobody reads the comma expression's
overall value here — only its side effects (incrementing i, decrementing
j) matter, which is by far the comma operator's most common real-world
use.
Quick practice
What does const age = 20; console.log(age >= 18 ? "adult" : "minor"); print?
What is 1 > 2 ? "a" : "b"?
What does let a = 1, b = 5; console.log((a++, b++, a + b)); print?
In for (let i = 0, j = 5; i < 3; i++, j--) { ... }, what does the comma operator let you do?
What does the comma operator return?
🎯 Your turn
Explain what for (let i = 0, j = 5; i < 3; i++, j--) { console.log(i, j); }
prints on each pass, and what role the comma operator plays in the update
clause i++, j--.
Write your explanation and submit it — there's no code to run here, just your reasoning.
