JavaScript Ternary Operator & IF - Else Statement

 



Ternary operator

Ternary operator can help you make your code easier to read while making it super effective when using a truthy or falsy value. This operator is the only JavaScript operator that takes in three operands. A condition followed by a question mark then an expression that executes if the statement is truthy, followed by a colon and an expression that executes if the statement is falsy.

    <script>

        var a = 100;
        var b;

        b = (a == 200)? "True" : "False";

        document.write(b);


    </script>

IF - ELSE Statement

The if a statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed.

  • if statements: if a condition is true, it executes a specific block of code.
  • else statements: if the same condition is false, it executes a specific block of code.
  • else if: this specifies a new test if the first condition is false.

        <script>

          var abc = 100;

          if(abc == 900){
            document.write("abc is greater");
          }else{
            document.write("abc is smaller");
          }

        </script>