Declare a temporary variable, copy the value, and then swap.
let a = 10;
let b = 20;
const temp = a;
a = b;
b = temp;
By using arrays and destructuring assignment, swapping can be done in a single line.
[b, a] = [a, b];
ยฉ 2025 juniyunapapa@gmail.com.