DEV Community

Chantae P.
Chantae P.

Posted on

1 1

Swapping Variables

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay