Open In App

What are Ternary Operator in JavaScript?

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The ternary operator in JavaScript is specified using a condition followed by a question mark and semi-colon (condition ? :). It shortens the decision-making process into a single line, consisting of a condition followed by two statements which are separated by a colon. If the condition is true, the expression after the question mark will be executed. Otherwise, the expression after the colon will be executed. This ternary operator enhances the code readability and reduces verbosity in those cases where simple choices are involved. It can be used as an alternative to the if-else statements in JavaScript.

Syntax:

condition expression ? statement1 : statement2;

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads