How to Use Multiple Conditional (Ternary) Operators in JavaScript

Posted by

Using Multiple Conditional (Ternary) Operators in JavaScript

Conditional operators, also known as ternary operators, allow you to evaluate a condition and then execute different code based on the result. These operators are often used to replace if/else statements, and can make your code more concise and efficient. In JavaScript, you can use multiple conditional operators to perform complex operations. In this tutorial, we’ll explain how to use multiple conditional operators in JavaScript.

Syntax of a Conditional (Ternary) Operator

A basic conditional operator in JavaScript consists of three parts: the condition, the true statement, and the false statement. Here’s the syntax:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

condition ? true_statement : false_statement

[/dm_code_snippet]

The condition is evaluated. If the condition is true, the true statement is executed. Otherwise, the false statement is executed. Let’s look at a simple example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 10;

x > 5 ? console.log('x is greater than 5') : console.log('x is less than or equal to 5');

// Output: x is greater than 5

[/dm_code_snippet]

In this example, the condition is x > 5. If the condition is true, the console.log() statement will be executed, and the message “x is greater than 5” will be printed to the console. Otherwise, the console.log() statement with the message “x is less than or equal to 5” will be printed to the console.

Using Multiple Conditional Operators

You can use multiple conditional operators to create complex conditions. Here’s the syntax for using multiple conditional operators:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

condition1 ? true_statement1 : (condition2 ? true_statement2 : false_statement)

[/dm_code_snippet]

Let’s look at an example. We’ll use multiple conditional operators to print messages to the console based on a value:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = 10;

x > 5 ? console.log('x is greater than 5') : 
  (x == 5 ? console.log('x is equal to 5') : 
    console.log('x is less than 5'));

// Output: x is greater than 5

[/dm_code_snippet]

In this example, the condition is x > 5. If the condition is true, the console.log() statement will be executed, and the message “x is greater than 5” will be printed to the console. Otherwise, the second condition (x == 5) is evaluated. If this condition is true, the console.log() statement with the message “x is equal to 5” will be printed to the console. Otherwise, the console.log() statement with the message “x is less than 5” will be printed to the console.

Conclusion

In this tutorial, we discussed how to use multiple conditional (ternary) operators in JavaScript. We showed you the syntax for using multiple conditional operators and provided an example. Now you have the knowledge you need to use multiple conditional operators in your own JavaScript code.