short-circuiting && - Does JavaScript have “Short-circuit” evaluation?

assignment advantages (2)

Possible Duplicate:
Using &&'s short-circuiting as an if statement?
Does javascript use optimization in boolean expressions?

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.


Yes, JavaScript has "short-circuit" evaluation.

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


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


JavaScript “strict” mode introduces in ECMAScript 5.

  (function() {
  "use strict";
   your code...

writing "use strict"; at the very top of your JS file turns on strict syntax checking. It does the following tasks for us :

(i) shows an error if you try to assign to an undeclared variable

(ii) stops you from overwriting key JS system libraries

(ii) forbids some unsafe or error-prone language features

"use strict" also works inside of individual functions. It is always a better practice to include "use strict in your code.

Browser Compatibility Issue: The "use" directives are meant to be backwards-compatible. Browsers that donot support them will just see a String literal that isn't referenced further. So, they will pass over it and move on.

javascript short-circuiting