2

I am reviewing some code for a JavaScript widget that I downloaded and there is a variable assignment similar to the following:

var a = a.something || a.somethingElse, c, d, e, f, g;

What I am wondering is if this is equivalent to this:

var a = a.something || a.somethingElse;
var c, d, e, f, g;

or this:

var a = a.something || a.somethingElse || c || d || e || f || g;

I have been reading up on short-circuit evaluation and assignments, and I understand that the first part is saying:

if ( a.something != (null or 0 or false)) {
    a = a.something;
} else {
    a = a.somethingElse;
}

but I cannot seem to find any resources that use an example like this one.


Thanks, for your help!

2
  • See MDN: var#Syntax Commented Jul 26, 2012 at 16:32
  • You forgot "", undefined and NaN :-) Commented Jul 26, 2012 at 16:40

1 Answer 1

4

Your understanding is correct. A comma seperated list in a var statement is treated like individual Ines.

As for the boolean OR statement: If the first expression is falsy, the second will be used. You can add more, as in your third example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much - exactly the confirmation that I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.