Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Expressions & Operators  ›  Lesson

Ternary and Comma Operators

Conditional Operators 12 minThe ternary operator condition ? ifTrue : ifFalse is a single expression that picks one of two values; the comma operator evaluates several expressions in order and yields only the last one, mostly seen in a for loop's update clause.

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.

A Y-junction: age >= 18 ? "adult" : "minor" forking to "adult" (true) with "minor" greyed out (false) — one fork, one value out; and a single lane (a++, b++, a + b) where each step runs in sequence and only the last value is the expression's result, mostly seen in a for loop's update clause

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:

javascript

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:

javascript

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:

javascript

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

quick drill

What does const age = 20; console.log(age >= 18 ? "adult" : "minor"); print?

quick drill

What is 1 > 2 ? "a" : "b"?

quick drill

What does let a = 1, b = 5; console.log((a++, b++, a + b)); print?

quick drill

In for (let i = 0, j = 5; i < 3; i++, j--) { ... }, what does the comma operator let you do?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne loop, two counters

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--.