How to use multiple ternary operators in a single statement in JavaScript ? Last Updated : 19 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, the ternary operator (? :) is a shorthand for if-else statements. You can use multiple ternary operators in a single statement to handle multiple conditions efficiently. This makes your code shorter, but be cautious as too many nested ternary operators can reduce code readability.Syntax:condition1 ?(condition2 ? Expression1 : Expression2) : Expression3;In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on condition2. If condition 2 is correct, then the output is Expression1. If it is incorrect, then the output is Expression2.Example 1: Using Multiple Ternary OperatorsIn this example, we are checking two conditions and using nested ternary operators to return the corresponding expression based on the conditions: JavaScript const age = 45; (age > 30) ? (age > 70) ? console.log("You are getting old") : console.log("You are between 30 and 69") : console.log("You are below 30"); OutputYou are between 30 and 69 Example 2: Another Example with Multiple ConditionsLet's look at another example where we use multiple ternary operators to determine a grade based on a score: JavaScript const num = 12; (num != 0) ? (num > 0) ? console.log("Entered number is positive") : console.log("Entered number is negative") : console.log("You entered zero"); OutputEntered number is positive Using multiple ternary operators in a single statement can simplify your code when handling multiple conditions. However, it is essential to balance code conciseness with readability to avoid making the logic too complex. Comment More infoAdvertise with us Next Article How to optimize the switch statement in JavaScript ? S shivam70 Follow Improve Article Tags : JavaScript Web Technologies javascript-operators JavaScript-Questions Similar Reads What does OR Operator || in a Statement in JavaScript ? JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be 6 min read How to Set Multiple Conditions in an If Statement in JavaScript? In JavaScript, conditional statements like if are used to execute code blocks based on certain conditions. There are situations where we might want to check multiple conditions within a single if statement. In such cases, JavaScript provides logical operators like && (AND) and || (OR) to com 5 min read Alternative to Multiple OR Operators in JavaScript In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to per 4 min read How to Use AND Statement in if with JavaScript? In JavaScript AND (&&) logical operator is used in the 'if' statement to check for two or more conditional validities. AND logical operator in the 'if' condition returns true only when all the conditions inside the 'if' statement are TRUE. If any one condition inside the 'if' statement is FA 2 min read How to optimize the switch statement in JavaScript ? The switch statement is necessary for certain programming tasks and the functionality of the switch statement is the same among all programming languages. Basically switch statements switch the cases as per the desired condition given to the switch. A switch statement can be used when multiple numbe 2 min read How to write an inline IF statement in JavaScript ? We can write an inline IF statement in javascript using the methods described below. Method 1: In this method we write an inline IF statement Without else, only by using the statement given below. Syntax: (a < b) && (your code here) Above statement is equivalent to if(a < b){ // Your c 2 min read Like