🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #19

JS Ternary


Example

let text = (age < 18) ?  "Minor" : "Adult";

Example

let isMember = true;

let discount = isMember ? 0.2 : 0;

Example

let isMember = false;

let discount = isMember ? 0.2 : 0;

Description

The conditional operator is a shorthand for writing conditional if...else statements.

It is called a ternary operator because it takes three operands.

Syntax

(condition) ? expression1 : expression2

Parameters

Col 1 Col 2
Parameter Description
condition Required. The condition to be tested. An expression that evaluates to true or false.
? Required. The operator separating the condition from the expressions.
expression1 Required. The value to return if the condition is true.
: Required. The operator separating the expressions.
expression2 Required. The value to return if the condition is false.

Note: The conditional (ternary) operator is the only JavaScript operator that takes three operands.


Browser Support

() ? x : y is an ES1 feature (JavaScript 1997).

It is fully supported in all browsers:

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS Ternary – FAQs

Quick answers about learning JS Ternary in JavaScript.

This free note from CodingNow 2.0 explains JS Ternary in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS Ternary, is 100% free with no signup required.
With focused practice, most students grasp JS Ternary in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now