Skip to content
20 Actually Useful JavaScript One-Liners (2026)

20 Actually Useful JavaScript One-Liners (2026)

DodaTech Updated Jun 20, 2026 4 min read

One-liners get a bad reputation as code-golf tricks. These aren’t that. Each one solves a concrete daily problem — deduplicating arrays, cloning objects safely, debouncing input handlers, or parsing URL parameters. They’re short enough to memorize, practical enough to use every day.

Arrays

Deduplicate an array

const unique = arr => [...new Set(arr)];
unique([1, 2, 2, 3, 3, 4]); // [1, 2, 3, 4]

Remove falsy values

const compact = arr => arr.filter(Boolean);
compact([0, 1, false, 2, '', 3]); // [1, 2, 3]

Shuffle an array (Fisher-Yates)

const shuffle = arr => arr.map(v => ({ v, r: Math.random() })).sort((a, b) => a.r - b.r).map(v => v.v);
shuffle([1, 2, 3, 4, 5]); // [3, 1, 5, 2, 4] (random)

Chunk into groups

const chunk = (arr, size) => arr.reduce((acc, _, i) => i % size === 0 ? [...acc, arr.slice(i, i + size)] : acc, []);
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]

Intersection of two arrays

const intersection = (a, b) => a.filter(v => b.includes(v));
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]

Difference of two arrays (in a but not in b)

const difference = (a, b) => a.filter(v => !b.includes(v));
difference([1, 2, 3], [2, 3, 4]); // [1]

Flatten one level

const flatten = arr => arr.flat();
flatten([[1, 2], [3, 4]]); // [1, 2, 3, 4]

Flatten deeply (any depth)

const deepFlatten = arr => arr.flat(Infinity);
deepFlatten([1, [2, [3, [4]]]]); // [1, 2, 3, 4]

Objects

Deep clone without libraries

const deepClone = obj => structuredClone(obj);
// Browser-native, handles dates, arrays, maps, sets, circular refs

Pick specific keys

const pick = (obj, keys) => Object.fromEntries(keys.filter(k => k in obj).map(k => [k, obj[k]]));
pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }

Omit specific keys

const omit = (obj, keys) => Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
omit({ a: 1, b: 2, c: 3 }, ['b']); // { a: 1, c: 3 }

Group by key

const groupBy = (arr, fn) => arr.reduce((acc, v) => { const k = fn(v); (acc[k] ??= []).push(v); return acc; }, {});
groupBy([6.1, 4.2, 6.3], Math.floor); // { 4: [4.2], 6: [6.1, 6.3] }

Sort by key

const sortBy = (arr, key) => arr.sort((a, b) => a[key] > b[key] ? 1 : -1);
sortBy([{ n: 3 }, { n: 1 }, { n: 2 }], 'n'); // [{ n: 1 }, { n: 2 }, { n: 3 }]

Deep freeze an object

const deepFreeze = obj => { Object.keys(obj).forEach(k => typeof obj[k] === 'object' && deepFreeze(obj[k])); return Object.freeze(obj); };

Strings & Values

Random hex color

const randomHex = () => `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, '0')}`;
randomHex(); // #a3f7c2

Copy to clipboard

const copy = text => navigator.clipboard.writeText(text);
copy("Hello"); // Copies to clipboard

Detect dark mode

const isDarkMode = () => window.matchMedia('(prefers-color-scheme: dark)').matches;
isDarkMode(); // true or false

Async & DOM

Wait / delay

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
await wait(1000); // Waits 1 second

Debounce (with leading option)

const debounce = (fn, delay) => { let t; return (...args) => { clearTimeout(t); t = setTimeout(() => fn(...args), delay); }; };

Throttle

const throttle = (fn, limit) => { let inThrottle; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; };

Parse URL parameters

const getParams = url => Object.fromEntries(new URL(url).searchParams);
getParams('https://example.com?a=1&b=2'); // { a: '1', b: '2' }
Is structuredClone() supported everywhere?
Yes — all modern browsers (Chrome 98+, Firefox 94+, Safari 15.4+) and Node 17+. It replaces JSON.parse(JSON.stringify(obj)) for deep cloning with better performance and support for Dates, Maps, Sets, and ArrayBuffers.
When should I use debounce vs throttle?
Debounce delays execution until after a pause — use for search-as-you-type inputs. Throttle limits execution rate — use for scroll or resize handlers where you want periodic updates.
Are there any limits to these one-liners?
Several skip edge-case validation for brevity. chunk uses reduce which creates intermediate arrays. debounce and throttle don’t handle this binding in all cases — pass arrow functions and they work fine. For production, consider lodash equivalents where correctness matters more than line count.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro