The Wayback Machine - https://web.archive.org/web/20241225114033/https://www.geeksforgeeks.org/javascript-ternary-operator/
Open In App

JavaScript Ternary Operator

Last Updated : 23 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Ternary Operator in JavaScript is a shortcut for writing simple if-else statements. It’s also known as the Conditional Operator because it works based on a condition. The ternary operator allows you to quickly decide between two values depending on whether a condition is true or false.

JavaScript
// JavaScript to illustrate Conditional operator

let PMarks = 50;

let res = (PMarks > 39) ? "Pass" : "Fail";

console.log(res);

Output
Pass

How Does the Ternary Operator Work?

The ternary operator works with three parts:

  • Condition: A statement that returns true or false.
  • Value if True: What happens if the condition is true?
  • Value if False: What happens if the condition is false?

Syntax:

condition ? trueExpression : falseExpression

JavaScript
// JavaScript to illustrate Conditional operator

let age = 60;

let res = (age > 59) ? "Adult" : "Not Adult";

console.log(res);

Output
Adult

Characteristics of Ternary Operator

  • The expression consists of three operands: the condition, value if true, and value if false.
  • The evaluation of the condition should result in either a true/false or a boolean value.
  • The true value lies between “?” & “:” and is executed if the condition returns true. Similarly, the false value lies after “:” and is executed if the condition returns false.

Nested Ternary Operators

The below code assigns a grade to marks using nested ternary operators. It checks ranges: <40 as “Unsatisfactory”, <60 as “Average”, <80 as “Good”, and >=80 as “Excellent”, then prints the result.

JavaScript
// JavaScript to illustrate 
// multiple Conditional operators

let marks = 95;
let res = (marks < 40) ? "Unsatisfactory" :
          (marks < 60) ? "Average" :
          (marks < 80) ? "Good" : "Excellent";

console.log(res);

Output
Excellent


Alternate Implementation:

JavaScript
let marks = 95;
let res = (marks > 80) ? 
          (marks > 90 ? "Excellent" : "Good") : 
          "Average";
console.log(res);

Output
Excellent


JavaScript Ternary Operator – FAQs

What is the ternary operator in JavaScript?

The ternary operator is a shorthand for the if-else statement. It takes three operands and is the only operator that takes three operands. It is used to evaluate a condition and return one of two values based on whether the condition is true or false.

What is the syntax of the ternary operator?

The syntax of the ternary operator is: condition ? expressionIfTrue : expressionIfFalse.

How does the ternary operator work?

The ternary operator evaluates the condition. If the condition is true, it returns expressionIfTrue; otherwise, it returns expressionIfFalse.

Can you use the ternary operator for multiple conditions?

Yes, you can nest ternary operators to handle multiple conditions. However, this can make the code hard to read, so it’s usually better to use if-else statements for complex conditions.

Is the ternary operator only used for returning values?

Primarily, the ternary operator is used for returning values based on a condition. However, it can also be used to execute code conditionally, but this is not recommended as it can make the code less readable.



Next Article

Similar Reads

three90RightbarBannerImg