Created
November 15, 2016 17:26
-
-
Save jasonbellamy/1f1f4ee735bfe75a977c00632fb9d926 to your computer and use it in GitHub Desktop.
Some black magic with the XOR operator to swap values.
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
function swap(a, b) { | |
a = a ^ b; | |
b = a ^ b; | |
a = a ^ b; | |
return [a, b]; | |
} | |
swap(1, 5); //=> [5, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment