JavaScript Ternary Operator
Last Updated :
23 Nov, 2024
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);
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);
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);
Alternate Implementation:
JavaScript
let marks = 95;
let res = (marks > 80) ?
(marks > 90 ? "Excellent" : "Good") :
"Average";
console.log(res);
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.