JavaScript(JS) JS swap two variables

https:‮ww//‬w.theitroad.com

here's how you can swap two variables using JavaScript:

let a = 10;
let b = 20;

// Before swapping
console.log("a =", a); // Output: a = 10
console.log("b =", b); // Output: b = 20

// Swapping variables
let temp = a;
a = b;
b = temp;

// After swapping
console.log("a =", a); // Output: a = 20
console.log("b =", b); // Output: b = 10

In this code, we have two variables a and b that we want to swap. We create a temporary variable temp to store the value of a before we overwrite it with the value of b. Then, we assign the value of b to a, and finally, we assign the value of temp (which is the original value of a) to b.

After swapping, the value of a becomes 20 and the value of b becomes 10, as expected.