Javascript comma operator

Posted by Claudiu on Stack Overflow See other posts from Stack Overflow or by Claudiu
Published on 2009-02-02T03:52:03Z Indexed on 2010/04/10 0:03 UTC
Read the original article Hit count: 435

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about comma