JavaScript ternary operator

 

js-ternary operator

JavaScript ternary operator

When you want to execute a block of code if a particular test evaluates to true, you often use the if-else statement. you can use the ternary operator as the shortcut for the if-else statement.

In general, the syntax of the ternary operator is as follows:

condition ? expression_1 : expression_2;

The JavaScript ternary operator is the only operator that takes three operands.

The condition is an expression that evaluates to a Boolean value, either true or false. If the condition is truethe ternary operator returns expression_1, otherwise it returns the expression_2.

The expression_1, and expression_2 are expressions of any type.

        <script>

        var a = 100;
        var b;
   
        b = (a == 100) ? "True" : "False";

        document.write(b);

    </script>