Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Ternary associativity

JavaScript’s conditional (ternary) operator selects one of two values from a condition. Chained ? expressions are grouped right-to-left, enabling compact multi-condition selection.

How it works

A conditional expression evaluates its condition and produces the value after ? when truthy or after : otherwise. When conditional operators are chained, they group from right to left, allowing an if/else-if/else-style value selection.

Syntax

condition ? value1 : value2

Parameters

NameTypeDescription
conditionexpressionExpression tested for truthiness.
value1expressionExpression returned when condition is truthy.
value2expressionExpression returned when condition is falsy.

Returns

The selected value: value1 when the condition is truthy, otherwise value2.

Key points

  • The conditional operator is called ternary because it accepts three operands: a condition and two result expressions.
  • A truthy condition selects the first result; a falsy condition selects the second.
  • Chaining conditional operators lets one expression choose among several values based on multiple conditions.
  • Its core form places the condition before ?, the truthy result between ? and :, and the fallback result after :.
  • Use it for compact value selection when each branch is an expression.

Common mistakes

  • Nested conditional expressions are intended to choose values; use if when the branches need to execute different statements.
  • Its low precedence affects grouping with comparisons, so a comparison can be written directly as the condition.

Example: Right-associative conditional expression

const score = 82;
const band = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(band); // B

The conditional operators associate from right to left, so this behaves like checking the first condition, then the second only if needed.

When to use it

Conditional assignment

Assigning a value based on one boolean or comparison.

Multi-condition value selection

Selecting one of several labels or values from ordered conditions.

Conditional return

Returning a computed value directly from a function.

Interview questions

QHow are chained ternary expressions grouped?

The conditional operator is right-associative. Therefore, a ? b : c ? d : e groups as a ? b : (c ? d : e), which supports an if/else-if/else-style chain.

Follow-up: What does right-associative mean here? It describes implicit grouping, not that every operand is evaluated immediately; the condition determines which result expression is selected.

QWhat value does a conditional expression produce?

The condition is evaluated first. If it is truthy, the expression after ? supplies the result; otherwise, the expression after : supplies it.

Follow-up: How would you make the grouping unambiguous? Add parentheses around the intended nested conditional, especially when the expression becomes difficult to read.

QWhat is the ternary operator's basic syntax and purpose?

The syntax is condition ? value1 : value2. The operator has three operands: a condition and two alternative expressions.

Follow-up: When should it be replaced with if? Use if when different branches need to perform statements rather than simply select one value.

  • if...else
  • Comparison operators
  • Logical operators
  • Operator precedence

Source: Conditional branching: if, '?'. Checked 2026-10-15.