10 JavaScript One-Liners Every Developer Should Know
Clean, practical JavaScript one-liners for everyday tasks — from array tricks to clipboard access. No libraries needed.
Codeyaan
May 10, 2026
10 JavaScript One-Liners Every Developer Should Know
These aren't just clever tricks — they're practical patterns you'll reach for daily.
1. Remove Duplicates from an Array
const unique = [...new Set(array)];
Works with primitives (strings, numbers). For objects, you'll need a different approach.
2. Generate a Random UUID
const uuid = crypto.randomUUID();
// "3b241101-e2bb-4d52-8f4a-6e9d2ed35c21"
Built into all modern browsers — no library needed.
3. Copy Text to Clipboard
await navigator.clipboard.writeText("Hello, clipboard!");
Requires user interaction (click/keypress) and HTTPS.
4. Shuffle an Array
const shuffled = array.sort(() => Math.random() - 0.5);
Good enough for most cases. For truly unbiased shuffling, use Fisher-Yates.
5. Get a Random Element
const random = array[Math.floor(Math.random() * array.length)];
6. Flatten a Nested Array
const flat = array.flat(Infinity);
// [1, [2, [3, 4]]] → [1, 2, 3, 4]
7. Check if an Element is in the Viewport
const isVisible = (el) => {
const r = el.getBoundingClientRect();
return r.top < window.innerHeight && r.bottom > 0;
};
8. Debounce a Function
const debounce = (fn, ms) => {
let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), ms); };
};
Okay, this one is two lines. But it saves you from installing lodash.
9. Deep Clone an Object
const clone = structuredClone(original);
Native deep cloning — handles dates, maps, sets, and circular references. Finally no more JSON.parse(JSON.stringify(...)) hacks.
10. Group an Array by Key
const grouped = Object.groupBy(items, (item) => item.category);
Object.groupBy is a newer API (2024+). Falls back to a reduce for older environments:
const grouped = items.reduce((acc, item) => {
(acc[item.category] ??= []).push(item);
return acc;
}, {});
These one-liners are the kind of thing you learn once and use forever. Bookmark this page and come back to it when you need a quick reference.
Want to format or test your JavaScript? Try our Code Formatter for instant Prettier formatting right in the browser.
Found this helpful? Share it:
Codeyaan
Writing about developer tools, web development, and making complex things simple.