Favorites

JavaScript ? : Conditional (ternary) Operator

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
? :
JavaScript Other OperatorThis operator is supported by all modern browsers.
Updated 28th November 2026

The conditional (ternary) operator picks one of two values based on a condition: condition ? valueIfTrue : valueIfFalse.

Description

The conditional operator, also called the ternary operator because it takes three operands, evaluates a condition and returns the second operand if it's truthy or the third if it's falsy. It's a compact alternative to if...else when you need a value, such as inside a template literal or JSX.

Ternaries can be chained to handle several cases, but deeply nested ones are hard to read; an if statement or a lookup object is often clearer.

It's the only JavaScript operator with three operands. Only the chosen branch is evaluated, so the other expression's side effects don't happen. Because it has low precedence, wrap it in parentheses when you use it inside a larger expression, such as string concatenation: 'You are ' + (age >= 18 ? 'an adult' : 'a minor').

Properties

Operator
? :
Category
Other
Precedence
2 (1 is lowest, 18 is highest)
Associativity
right-to-left

Syntax

condition ? exprIfTrue : exprIfFalse

Example

const age = 20;
console.log(age >= 18 ? 'adult' : 'minor');

const count = 1;
console.log(`${count} item${count === 1 ? '' : 's'}`);

const score = 72;
console.log(score >= 80 ? 'A' : score >= 60 ? 'B' : 'C');

Browser Support

The following information will show you the current browser support for the ? : operator. Hover over a browser icon to see the version that first introduced support for this operator.

This operator is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee