Does JavaScript have "Short-circuit" evaluation?

GibboK picture GibboK · Sep 23, 2012 · Viewed 49.2k times · Source

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.

Answer

gdoron is supporting Monica picture gdoron is supporting Monica · Sep 23, 2012

Yes, JavaScript has "short-circuit" evaluation.

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

Live DEMO

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

Live DEMO