Today I Learned

A Hashrocket project

Swap JavaScript Array Items Inline

Today while doing an Exercism, I learned that through ES6 destructuring we can swap two array items in one line of code. Here’s how it works:

[array[0], array[1]] = [array[1], array[0]]

Items one and two are swapped in array. Modify those values in-place like a boss.

Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.