So I am taking a Udemy Web Developer course. I am currently learning JavaScript right now and came across an exercise problem that may have stumped a lot of people including myself. Which is swapping variables. The goal is to swap variables using three lines of code(or less) without changing the code,cannot add numbers, and you can't redeclare the variables. Here's the problem:
let a = 3;
let b = 8;
a should equal 8 and b should equal 3.
Solution 1 Using an array:
[a, b] = [b, a];
Solution 2 declaring a new variable:
let c = a;
a = b;
b = c;
Top comments (0)