Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Last active November 13, 2017 14:40
Show Gist options
  • Save aderaaij/b8ade4aebaabf990f6682beb54af04f7 to your computer and use it in GitHub Desktop.
Save aderaaij/b8ade4aebaabf990f6682beb54af04f7 to your computer and use it in GitHub Desktop.
A neat trick we can do with es6 array destructuring is swapping out values of two variables without the need of a third temporary variable. Example from es6.io
let inRing = 'Hulk Hogan';
let onSide = 'The Rock';
console.log(inRing, onSide);
/* This might look a bit funky but what's happening is pretty simple: On the right
hand side we've got an array with the original values. As we give an array, we can
destructure it, which we do on the lefthand side. We destructure the array renaming
the `onSide` value to `inRing`and the `inRing` value to `onSide`*/
[inRing, onSide] = [onSide, inRing];
console.log(inRing, onSide);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment