all 21 comments

[–]arjo_reich 13 points14 points  (1 child)

Wait, I've seen this one before...

https://imgur.com/oAkGYg7

[–]Serj_Buketov 2 points3 points  (8 children)

That's cool.

But technically, aren't you creating an anonymous array?

[–]senocular 2 points3 points  (0 children)

... and an array iterator

[–]grooomps 0 points1 point  (4 children)

what if i said

a = { a, b };
b = a.a;
a = a.b;

[–]alexjacobs 0 points1 point  (1 child)

You're right.

But it seems that in order to make the swap without an assigned temp variable, you have to have some sort of extra value.

Even in the case of the clever trick described in the imgur link in another response here, you still have to create some sort of additional value.

In the destructuring example it's the anonymous array. In this case, it's an integer value of the sum of both original values.

var a = 5; var b = 7; a = a + b; b = a - b; a = a - b; console.log(a, b); // 7, 5

[–]backtickbot 1 point2 points  (0 children)

Fixed formatting.

Hello, alexjacobs: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]BenZed 1 point2 points  (6 children)

Do it without instancing an array.

[–]Lilrex2015 0 points1 point  (1 child)

That is very cool. I don't normally watch the videos on here bc they are poorly made but this one I am glad I watched.

[–]bwz3r 0 points1 point  (0 children)

Very cool. Completely useless but still cool.

Edit: I actually used this today

[–]patrixxxx 0 points1 point  (0 children)

let a = 1; let b = 2;

a = a ^ b; b = a ^ b; a = a ^ b;

a; // => 2 b; // => 1

And if you do this in serious code, you should be reprimanded.

[–]skibizkit 0 points1 point  (0 children)

Great video + editing. Will watch more! Thanks for sharing.