Ternary Operator in javascript

Ternary Operator in javascript

Ternary operator in JavaScript programming is also known as conditional operator. Ternary operator is also treated as if-else statement method alternative in short. Generally, ternary operator mainly has three operands, and ternary operator is the only built-in operator in JavaScript programming. Which is ternary i.e., deals with immediate 3 logic values.

Ternary Operator in javascript

General ternary operator syntax in JavaScript.

condition ? expression_if_true : expression_if_false;

Ternary operator analysis.

  • ternary condition – It is an expression in the current ternary operator, which will be evolved by the ternary operator. Here if this expression evolves to true, then it prints the related statement.
  • expression true – It is the value in the current ternary operator, which returns the value if the active condition is true.
  • Expression false – This is the value in the current ternary operator which returns the value if the condition is false.

Ternary operator syntax.

condition ? logicexpr1 : logicexpr2;

Here the condition in the current ternary operator is evolved.

If the condition is true, then the logicexpr1 part is executed.

If the condition is false, then the logicexpr2 part is executed.

Example of ternary operator in JavaScript.

let age = 21;

let info = (age >= 21) ? “You are able to vote” : “You are not able to vote”;

console.log(info); // Result – “You are able to vote”

Here in this example.

In the above logic expression, the age condition (age >= 21) is true, so the information “You are able to vote” is defined to the variable info.

Using ternary operator for assignment Example.

let integer = 22;

let output = (integer % 2 === 0) ? “Even Number” : “Odd Number”;

console.log(output); // Result – “Even Number”

Here in this example.

Here in this condition (integer % 2 === 0) expression checks whether the current integer is an even number or not. As the programmer has already declared the integer variable as 22, which is an even number, so it returns the default “Even Number” value message, and its result is assigned to the output variable.

Nested Ternary Operator Example in JavaScript.

In JavaScript programming, you can apply the ternary operator as nested expression logic to test multiple ternary operators, but always remember, because it makes your program code complex and difficult to debug error free.

let number = 95;

let grade = (number >= 90) ? “A” :

(number >= 80 && number <= 70) ? “B” :

(number >= 70 && number <= 60 ) ? “C” :

(number >= 60 && number <= 50 ) ? “D” : “F”;

(number <= 50 ) ? “F”:

console.log(grade); // Result – “A”

Here in this example.

The first condition checks whether the number is >= 90 or not. If the condition is false, it checks the next condition where (number >= 80), and so on for the remaining conditions and logic. As here

number is 95, so here grade prints “A” as the variable grade.

Example of ternary operator with function in JavaScript.

You can use ternary operator inside the function based on your program development requirement.

function checkweather(temp) {

return (temp >= 48) ? “Very hot temprature” : “Very cold temprature”;

}

console.log(checkweather(48)); // Result – “Very hot temprature”

console.log(checkweather(18)); // Result – “Very cold temprature”

Advantages of using ternary operator in JavaScript.

  • Concise Operators – The ternary operator in JavaScript programming allows you to create program logic conditions in a much more compact form than a complete if-else statement block.
  • Inline Assignment Option – The ternary operator is required when you want to assign values ​​in a single row based on a logic expression.

Limitations and Considerations of the Ternary Operator.

  • Readability – In any JavaScript program, the ternary operator can make your program code more detailed, and can also reduce readability in existing programs, especially when used in nested order. When the given condition expression is simple, use the ternary operator only as per the need and do not make the ternary operator more complex with conditions.
  • Complexity – For more complex condition logic expressions in JavaScript programs, if-else statement expressions should be used.

Example of comparing ternary operator with if-else statement.

Here is a simple example of comparing ternary operator with if-else statement in JavaScript programming.

let integer = 2;

// explain with if-else statement

if (integer > 1) {

console.log(“integer is Greater than 1”);

} else {

console.log(“integer is Less than or equal to 1”);

}

// apply same above if else condition expression Using the ternary operator

console.log(integer > 1 ? “integer is Greater than 1” : “integer is Less than or equal to 1”);

Here in both the above examples the output will be “integer is Greater than 1”, but here the ternary operator deals with it in a more compact way.

Conclusion of JavaScript Ternary Operator.

The ternary operator in any JavaScript program is a convenient way to deal with any program logic condition expression in a single line. Always remember, the ternary operator should be used judiciously, as using it in more complex nesting orders or highly complex programming conditions can impact the readability of your code. You can always use ternary operator for simple direct condition. It is better option to use ternary operator.