This project has moved and is read-only. For the latest updates, please go here.

Should single-line variable swapping work?

Dec 14, 2010 at 8:51 PM
Edited Dec 14, 2010 at 8:52 PM


I came across this link which talks about the later versions of ECMAScript supporting single-line variable swapping like this:

a = 1;

b = 2;

[a, b] = [b, a];

This makes a equal to 2 and b equal to 1 without having to have an intermediate variable on the user's part to do the swap.

Did this make it into ECMAScript 5 level or is it beyond that?

Here's a reference link of someone else who mentioned it but not whether or not it was a level 5 thing:


Dec 15, 2010 at 2:01 AM

This feature is called destructuring assignment, and it didn't make it into ECMAScript 5.  However, it has been approved for ECMAScript Harmony, which presumably will be the next major version of the language.

See for more information.

- Paul