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];
In JavaScript, the double exclamation mark (!!) operator is used to convert a value to a boolean.
const a = [1, 2, 3];
const is_a_truthy = !!a; // true
const b = null;
const is_b_truthy = !!b; // false
const c = 0;
const is_c_truthy = !!c; // false
const d = "0";
const is_d_truthy = !!d; // true
ยฉ 2025 juniyunapapa@gmail.com.