DEV Community

luisguillermobultetibles
luisguillermobultetibles

Posted on

Interchange numerical values without temporal's

This excercises introduce you to a dark idea of what is information, at least for me, let's see:

supouse that a = 2 and b = 3

Do that

let a = a + b;
let b = a - b;
let a = a - b;

Is done, y así intercambias dos valores numéricos sin intermediarios, Luis. Chao.

Top comments (1)

Collapse
 
loucyx profile image
Lou Cyx

That actually doesn't work because you're redeclaring a and b:

let a = 2;
let b = 3;
let a = a + b; // Uncaught SyntaxError: Identifier 'a' has already been declared
let b = a - b;
let a = a - b;
Enter fullscreen mode Exit fullscreen mode

So you might actually do this:

let a = 2;
let b = 3;
a = a + b;
b = a - b;
a = a - b;
Enter fullscreen mode Exit fullscreen mode

And yet, that's a lot code when nowadays you can simply do this:

let a = 2;
let b = 3;
[a,b] = [b, a];
Enter fullscreen mode Exit fullscreen mode

Cheers!