Javascript Short circuiting operators

Below is the example of the Short circuiting operators.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
        function gfg() { 
              // AND short circuit
             document.write(false && true
             document.write("</br>");
             document.write(true && true
             document.write("</br>");
              // OR short circuit
             document.write(true || false
             document.write("</br>");
             document.write(false || true
        
        gfg(); 
    </script>

    chevron_right

    
    

  • Output:
    false
    true
    true
    true
    

In JavaScript short-circuiting, an expression is evaluated from left to right until it is confirmed that the result of the remaining conditions is not going to affect the already evaluated result. If the result is clear even before the complete evaluation of the expression, it short circuits and the result will be returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing.

AND(&&) short circuit: In case of AND, the expression is evaluated until we get one false result because the result will always be false, independent of the further conditions. If there is an expression with &&(logical AND), and the first operand itself is false, then a short circuit occurs, the further expression is not evaluated and false is returned.

Example: Short circuiting using AND(&&) operator.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// Since first operand is false and operator
// is AND, Evaluation stops and false is
// returned.
console.log(false && true && true && false)
  
// Whole expression will be evaluated.
console.log(true && true && true)
</script>

chevron_right


Output:



false
true

OR(||) short circuit: In case of OR, the expression is evaluated until we get one true result because the result will always be true, independent of the further conditions. If there is an expression with ||(logical OR), and the first operand itself is true, then a short circuit occurs, evaluation stops, and false is returned.

Example: Short circuiting using OR(||).

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
// First operand is true and operator is ||, 
// evaluation stops and true is returned.
console.log(true || false || false)
  
// Evaluation stops at the second operand(true).
console.log(false || true || true || false)
</script>

chevron_right


Output:

true
true

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.