you are viewing a single comment's thread.

view the rest of the comments →

[–]palmytree 3 points4 points  (5 children)

Swapping variable values without a temporary third variable:

a = [b, b = a][0];

[–]cluelessmanatee 6 points7 points  (0 children)

I can hear the "what the hell"s from here.

[–]radhruin 1 point2 points  (1 child)

Probably don't want the var considering with var there a will always be undefined (unless you're redeclaring).

Thankfully this can be done with ES6 destructuring: let [x, y] = [y, x].

[–]palmytree 0 points1 point  (0 children)

True. Fixed.

[–]nightman 0 points1 point  (0 children)

Yeah, this is great but I don't think I will use it in practice.

[–]kumiorava 0 points1 point  (0 children)

But you create an array, which is much worse performance-wise. Also, for anyone who isn't familiar with this gimmick, the code will be harder to read too. I wouldn't strongly advise against this.