Nested Ternary Operator in JavaScript17 Mar 2025 | 5 min read We will understand the nested ternary operator JavaScript in this article. Ternary operator
Syntax:In the above syntax, first of all conditional expression is written then a ternary operator (?:) is used then an expression is written that checks whether the conditional expression is true or false. If the expression is true then the statement written after question mark (?) is executed. If the expression is not satisfied, that means it is false then the statement written after colon (:) is executed. Parameters of ternary operatorcondition: It is an expression that is utilized as a value. expression_if_true: It is an expression that is utilized to check whether the condition is true or not. expression_if_false: This expression is executed when the condition is false. First of all, we will create a program on how to check whether the candidate is eligible to vote or not utilizing if else statement. Code: Output: We can witness the output that the candidate is not eligible to vote. ![]() Now, we will create the same program utilizing the ternary operator and see the difference. Code: We can witness that the code becomes smaller and is contained in one line. So, it is one of the best advantages of using the ternary operator. Explanation: In the above code, first of all, the condition is given which consists of a variable called "age" which is assigned a "15" value. After that, condition expression is given which checks the whether the age is greater or equal to 18 or not. As the age is not greater or equal to "15" that means the statement is not true. Now, the next part written after colon is executed and prints that the candidate is eligible for vote. Output: We can witness in the output that the condition is not satisfied so the candidate is not eligible for vote. ![]() Let us utilize the ternary operator to create a program that provides ratings to the students according to their marks obtained. Explanation: In the above code, first of all condition is given which consists of a variable called "score" which is assigned an 85 value. After that, conditional expression checks whether the score is greater or equal to 80 or not. As the score is greater than 80 that means the statement is true so it prints the "Excellent". Output: We can witness in the output that the condition is true. ![]() We have understood the ternary operator. Now, we will understand the nested ternary operator. Nested ternary operatorTernary operators can be nested similar to the nested if-else block. They are used to take more sophisticated decision. It is like ternary operator inside another ternary operator. Let us first construct a program to find the biggest number from four given numbers utilizing the nested if-else statement. Code: Output: We can witness the output displays the biggest number. ![]() Now, we will construct the same program utilizing the nested ternary operator. Code: We have noticed that the line of code is less then nested ternary operator as compared to nested if-else statement. Explanation: In the above code, first of all, three variables called "num1", "num2" and "num3" are declared and assigned values 45, 12 and 15 respectively. Then, the ternary operator chain is utilized to find the biggest number. The first condition checks whether the num1 is greater or equal to num2 or not. If num1 is greater than equal to num2, then nested condition checks whether the num1 is greater or equal to num3 or not. If true then num1 is the biggest number. If false then num3 is the biggest number. If num1 is not greater than equal to num2 then it means num2 is greater. After that nested condition checks whether the num2 is greater or equal to num3 or not. If true then num2 is the biggest number. If false then num3 is the biggest number. Output: We can witness the output displays the largest number found utilizing the nested ternary operator. ![]() Let us construct a program that checks the air quality and gives the output based on the conditional statement. Code: Explanation: In the above code, first of all, a variable called "air_quality_index" is declared and assigned a value of 51. After that, the ternary operator chain is utilized to determine the air quality. The first condition checks whether the air_quality_index is greater than 300 or not. If true then the outcome will be "Air quality is worst." If false then the nested condition checks whether the air_quality_index is greater than 200 or not. If true then the outcome will be "Air quality is bad." If false then another nested condition checks whether the air_quality_index is greater than 100 or not. If true then the outcome will be "Air quality is moderate."; otherwise, the "Air quality is good." Output: We can witness that the output displays the air quality utilizing the nested ternary operator. ![]() Conclusion:We have understood the nested ternary operator in JavaScript in this article. We have understood the ternary operator and nested ternary operator by comparing them with the if-else and nested if-else. Next TopicV8-engine-in-javascript |
What is Sandbox in JavaScript? In JavaScript, a sandbox is a security implementation that helps us limit the capabilities of the JavaScript code running inside it. It is regularly used in the user and server-side environment to execute the code that cannot be relied on and ent...
4 min read
In JavaScript, a fixed collection of named values is represented via enums. They enable programmers to create a list of named constants that may be used across the codebase to improve error ention, readability, and maintainability. Enumerations, often known as enums, are used in JavaScript to...
11 min read
The {X, Y} quantifier contains and matches numbers from the x to y range. The regex quantifier operates in a string of numbers with the regular expression's search, match, test, and exec method. We can use regex modifiers for the available character with the quantifier. Syntax To...
4 min read
JavaScript, as one of the most broadly utilized programming languages, offers developers a heap of instruments and functions to successfully handle asynchronous tasks. One such instrument is the then() function, an urgent part of JavaScript's Promise Programming interface. The then() function assumes a vital part...
6 min read
When it comes to web development, HTML, CSS, and JavaScript are the three most important languages. Having the correct Integrated Development Environment (IDE) may greatly increase productivity and optimize your workflow, regardless of the complexity of the web application you're developing or the simple webpage...
8 min read
JavaScript TextEncoder and TextDecoder are the functions to encode and decode the given value, respectively. The TextEncoder works to convert a specified string into the utf-8 standard using the JavaScript function. It transforms the string into a Uint8Array. Using a JavaScript function, the TextDecoder converts...
6 min read
You may want to include the copy feature while creating complex web pages and applications. Instead of selecting the text and pressing a few keys on the keyboard to copy it, your users may now copy text by clicking a button or icon. When someone has...
3 min read
JavaScript instanceof operator will find the object type at the time of runtime. The outcome we can expect can be Boolean values that are either true or false which are based on the inputs we gave and the objects that are included. Syntax: var instance = objectName...
4 min read
What is ? Zod is a JavaScript and typescript library that helps developers to define the schema for their data. Schema is a blueprint that describes the shape and constraints of data in this context. Zod helps the developers to define these schemas concisely and then use...
6 min read
Introduction JavaScript's Grid is an effective tool for web developers that make it simple to design intricate layouts. There is a rising demand for responsive and adaptable layouts as websites get more complex. One way to solve this problem is with Grid JavaScript, which lets programmers...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India