Skip to content

Instantly share code, notes, and snippets.

@belinwu
Last active August 29, 2015 14:21
Show Gist options
  • Save belinwu/3a3c3229db0ddb345332 to your computer and use it in GitHub Desktop.
Save belinwu/3a3c3229db0ddb345332 to your computer and use it in GitHub Desktop.
Swap Two Variables Without Using a Temp Variable (With Math!)
var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
console.log("a = " + a + ", b = " + b); // a = 20, b = 10
var a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
console.log("a = " + a + ", b = " + b); // a = 20, b = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment