Last active
August 29, 2015 14:21
-
-
Save belinwu/3a3c3229db0ddb345332 to your computer and use it in GitHub Desktop.
Swap Two Variables Without Using a Temp Variable (With Math!)
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
var a = 10, b = 20; | |
a = a + b; | |
b = a - b; | |
a = a - b; | |
console.log("a = " + a + ", b = " + b); // a = 20, b = 10 |
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
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