Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created July 4, 2010 23:19
Show Gist options
  • Save dshaw/463848 to your computer and use it in GitHub Desktop.
Save dshaw/463848 to your computer and use it in GitHub Desktop.
// From @izs /via @janl
// http://twitter.com/izs/status/17744109574
// JavaScript one-liner to swap two variables:
foo = [bar, bar = foo][0];
@dshaw
Copy link
Author

dshaw commented Jul 5, 2010

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment