0

I've two variables and i wanna swap them without using third variable.

Ex:

let a = 'value1',
    b = 'value2';

Result:

a = 'value2',
b = 'value1'

is this possible?

Umair Ahmed
  • 8,337
  • 4
  • 29
  • 37
  • 1
    Yes it is. But telling you how would defeat the purpose of the assignment I think. Besides, this is easily googleable. – Felix Kling Oct 26 '16 at 16:35
  • 1
    @FelixKling Unfortunately the ES6 answer in that dup is well-hidden down past the old accepted answer. I wonder if it would be better to re-open this, give it a modern answer, then mark the other question as a dup of this one, since believe it or not I can not find any other questions on the topic here. –  Oct 26 '16 at 17:27
  • @FelixKling this is es6 question not a normal javascript. – Umair Ahmed Oct 26 '16 at 21:16
  • All the suggestions in the duplicate are valid in ES6 as well, it even has ES6 specific solutions, you just have to look through the answers: http://stackoverflow.com/a/25910841/218196 – Felix Kling Oct 26 '16 at 21:22
  • Huh finally i found the best way for swapping variables value in ES6. i'm going to use new feature of ES6 called destructuring. using destructuring: `let a = 'value1', b='value2'; [a, b] = [b, a];` and it's done. – Umair Ahmed Oct 26 '16 at 21:26

0 Answers0