javascriptshort-circuiting

Does JavaScript have "Short-circuit" evaluation?


I would like to know if JavaScript has "short-circuit" evaluation like &&-operator in C#. If not, I would like to know if there is a workaround that makes sense to adopt.


Solution

  • Yes, JavaScript has "short-circuit" evaluation.

    if (true || foo.foo){
        // Passes, no errors because foo isn't defined.
    }
    

    Live demo

    if (false && foo.foo){
        // Also passes, no errors because foo isn't defined.
    }
    

    Live demo