In JavaScript, the ternary operator ? : allows you to write more concise and readable code when dealing with multiple conditions. It is a short way to write an if-else statement.
Syntax
The syntax of the ternary operator is as follows:
condition ? expression1 : expression2
- If the condition is true,
expression1is executed. - If the condition is false,
expression2is executed.
Example
Let’s say we have a variable age that holds a person’s age and we want to check if they are old enough to vote. We can use a ternary operator to determine the result:
const age = 18;
const canVote = age >= 18 ? "Yes" : "No";
console.log(canVote); // Output: "Yes"
In this example:
- The condition is
age >= 18, which checks if the age is greater than or equal to 18. - The expression
Yesis executed if the condition is true. - The expression
Nois executed if the condition is false.
Handling Multiple Conditions
You can also use nested ternary operators to handle multiple conditions. Let’s say we want to check if a number is positive, negative, or zero:
const num = 10;
const result = num > 0 ? "Positive" : num < 0 ? "Negative" : "Zero";
console.log(result); // Output: "Positive"
In this example:
- The first condition
num > 0checks if the number is greater than 0. - If the first condition is true, the expression
"Positive"is executed. - If the first condition is false, the inner ternary
num < 0 ? "Negative" : "Zero"is evaluated. - If
num < 0is true, the expression"Negative"is executed. - If
num < 0is false, the expression"Zero"is executed.
Conclusion
Ternary operators provide a shorthand way to handle multiple conditions in JavaScript. They allow you to write more compact and readable code by condensing if-else statements. However, it’s important to use them judiciously to maintain code readability and avoid complex nested ternaries.
#programming #javascript