0

When combining assignment with comma (something that you shouldn't do, probably), how does javascript determine which value is assigned? Consider these two snippets:

function nl(x) { document.write(x + "<br>"); }
var i = 0;
nl(i+=1, i+=1, i+=1, i+=1);
nl(i);

And:

function nl(x) { document.write(x + "<br>"); }
var i = 0;
nl((i+=1, i+=1, i+=1, i+=1));
nl(i);

The first outputs

1
4

while the second outputs

4
4

What are the parentheses doing here?

1 Answer 1

3

I was confusing two things, here. The first call to 'nl' is a function call with four arguments. The second is the evaluation of the comma into one argument.

So, the answer: the value of a list of expressions separated by ',' is the value of the last expression.

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

1 Comment

Well, at least you have a nice little "what does this code do" question now. :)

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.