Created
July 4, 2010 23:19
-
-
Save dshaw/463848 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From @izs /via @janl | |
// http://twitter.com/izs/status/17744109574 | |
// JavaScript one-liner to swap two variables: | |
foo = [bar, bar = foo][0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var foo = 1;
var bar = 2;
var tempArray = [bar, bar = foo]; // value of foo assigned to bar and resulting value added to the array... but we ignore it. Note: it's generally poor form do assignment in this context.
foo = tempArray[0]; // grab the old value of bar from the array and assign it to foo.
console.log(tempArray);
console.log(foo);
console.log(bar);