Created
July 5, 2010 02:26
-
-
Save cheeaun/463940 to your computer and use it in GitHub Desktop.
JavaScript one-liner to swap two variables
This file contains 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
// via http://twitter.com/izs/statuses/17744109574 | |
var foo = 1; | |
var bar = 2; | |
foo = [bar, bar = foo][0]; |
swap(x,y){ y = (function(x){ return x })(x, x=y); }
ES6+:
swap(x,y){ return [y,x] }
ES6 is much better.
let x=1
let y=2
{x,y} = {x:y, y:x}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 5 has a typo in it.