3

This question has a few answers (sort of) already: Javascript: var = var = function, Multiple Variable Assignments in one row, etc. but I would like to ask a different kind of question about it.

So, this is quite handy to be able to use:

var av = 0, bb;
var cvs = bb = av;
console.log(cvs,bb) // outputs 0 0

My question is, how cross-browser is this? Can I get away with using this everywhere, including IE6 or whatnot, or should I just stick to:

var av = 0, bb;

bb = av; cvs = av;

And if you change one variable at any point in the code, does that mean that the the other variable is changed too, or is there no connection outside of the initial assignment?

Thanks!

1
  • 2
    It's part of JavaScript so it should work in browsers which implements JavaScript Commented Feb 20, 2014 at 22:44

2 Answers 2

1

You can rely on this behavior, it is a part of the ECMAScript standard. You can check out the exact details here:
ECMAScript Language Specification (5.1) - Simple Assignment

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

Comments

0

To answer your second question:

And if you change one variable at any point in the code, does that mean that the the other variable is changed too, or is there no connection outside of the initial assignment?

That's easy to test:

var av = 0, bb;
var cvs = bb = av;
console.log(av, cvs, bb); // outputs 0 0 0

cvs = 1;
console.log(av, cvs, bb); // outputs 0 1 0

bb = 2;
console.log(av, cvs, bb); // outputs 0 1 2

av = 3;
console.log(av, cvs, bb); // outputs 3 1 2

So there is no connection outside the initial assignment.

1 Comment

This is not true. Objects are passed by references in this case.

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.