Skip to content

Instantly share code, notes, and snippets.

@abhardwaj4
Created November 4, 2016 18:52
Show Gist options
  • Save abhardwaj4/49ec34eb2d4fa167ff4d287590452128 to your computer and use it in GitHub Desktop.
Save abhardwaj4/49ec34eb2d4fa167ff4d287590452128 to your computer and use it in GitHub Desktop.
Swapping two variables in javascript
function swapUsingThirdVariable(a, b) {
var c = a;
a = b;
b = c;
}
function swapNumbersWithoutThirdVariable(a, b) {
a = a + b;
b = a - b;
a = a - b;
}
function swapUsingES6Destructuring(a, b) {
[a, b] = [b, a];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment