Last active
November 13, 2017 14:40
-
-
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
This file contains hidden or 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
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