Syntax

JavaScript Ternary Operator Syntax

In JavaScript, ternary operator: picks one of two values in one line — const label = ok ? "Yes" : "No";

Quick Answer

Syntax
const label = ok ? "Yes" : "No";
JavaScriptSyntax

Ternary Operator

Code example
const label = ok ? "Yes" : "No";

Picks one of two values in one line.

Learn More

How to ternary operator in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Ternary Operator Syntax

Use const label = ok ? "Yes" : "No";.

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for ternary operator?

Use this pattern: const label = ok ? "Yes" : "No";

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify