28 JavaScript One-Liners every Senior Developer Needs to Know

Mate Marschalko
9 min readMar 1, 2024

--

1. Swapping Values Without a Temporary Variable

let a = 1, b = 2;

[a, b] = [b, a];

// Output: a = 2, b = 1

This one-liner uses array destructuring to swap the values of a and b without needing a temporary variable. It's a neat trick that makes your code cleaner and more concise. The [a, b] = [b, a]

--

--

Mate Marschalko

Senior Creative Developer, Generative AI, Electronics with over 15 years experience | JavaScript, HTML, CSS