DEV Community

Cover image for How to Swap Variables' values Using Array Destructuring
Islam Sayed
Islam Sayed

Posted on

3

How to Swap Variables' values Using Array Destructuring

You can use array destructuring for easy swapping the values of 2 variables together. Here is how ๐Ÿ‘‡

Sie kรถnnen die Array-Destrukturierung verwenden, um die Werte von 2 Variablen einfach zu vertauschen. Hier ist, wie ๐Ÿ‘‡

let x = 1;
let y = 5;
[x, y] = [y, x];
console.log(x); //5
console.log(y); //1

Top comments (2)

Collapse
 
esharmony profile image
Matt Barnden โ€ข

Can you point me to anything to help me understand how this works. I understand what you are doing, but just don't understand the mechanics of it, what is happening in terms of memory etc.

Collapse
 
isalahyt profile image
iSalah-YT โ€ข

thank u so much

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay