10 JavaScript One-Liners That Will Make You Look Like a Pro
These 10 powerful JavaScript one-liners will simplify your code and impress your team. From array magic to object tricks, practical snippets you'll actually use every day.

Every experienced JavaScript developer has a toolkit of snippets they reach for daily. These aren't clever tricks for the sake of being clever; they're practical, readable, and genuinely useful patterns that save time and reduce bugs.
Here are 10 one-liners you'll actually use in real projects.
- Remove Duplicates from an Array
const unique = [...new Set(array)];
No loops. No filters. Just spread a Set and you're done. Works with strings, numbers, and any primitive type.
This replaces 5+ lines of traditional duplicate removal logic with a single, readable expression.
- Shuffle an Array
const shuffled = array.sort(() => Math.random() - 0.5);
Need a quick randomization for a quiz app, card game, or randomized list? This does the job. For truly uniform distribution in production, use the Fisher-Yates algorithm, but for everyday use, this is clean and fast.
- Flatten a Nested Array
const flat = array.flat(Infinity);
Got arrays inside arrays inside arrays? flat(Infinity) handles any depth. No recursive function needed.
// Before [[1, 2], [3, [4, 5]], [6]]
// After [1, 2, 3, 4, 5, 6]
- Convert Any Value to a Boolean
const isTrue = !!value;
The double bang operator. It converts any value to its Boolean equivalent. Useful in conditionals, filtering, and data validation.
- !!"" → false
- !!"hello" → true
- !!0 → false
- !!null → false
- !!undefined → false
- !![] → true
- Get a Random Element from an Array
const random = array[Math.floor(Math.random() * array.length)];
Perfect for random quotes, random colors, random tips, anything where you need to pick one item from a list.
- Swap Two Variables Without a Temp
[a, b] = [b, a];
ES6 destructuring makes variable swapping elegant. No temporary variable needed. This works with any data type.
One of those patterns that looks magical the first time you see it, then becomes second nature.
- Conditionally Add Properties to an Object
const user = { name: "Alex", ...(isPremium && { badge: "gold" }) };
This is incredibly useful when building API payloads or configuration objects. If isPremium is false, the badge property simply doesn't exist — no undefined values cluttering your object.
- Get the Last Element of an Array
const last = array.at(-1);
Forget array[array.length - 1]. The .at() method accepts negative indices, making this clean and readable. Works in all modern browsers and Node.js.
- Generate a Random Hex Color
const color = "#" + Math.random().toString(16).slice(2, 8).padEnd(6, "0");
Need a random color for a generative art project, placeholder UI, or testing? This generates a valid hex color code every time.
- Check if an Object is Empty
const isEmpty = Object.keys(obj).length === 0;
There's no built-in isEmpty() in JavaScript. This is the cleanest way to check. Works reliably with plain objects.
Pro tip: If you're checking API responses, combine this with a null check: !obj || Object.keys(obj).length === 0
When to Use One-Liners (and When Not To)
One-liners are powerful, but readability always wins over cleverness. Use these when:
- The intent is immediately clear
- Your team is familiar with the pattern
- It replaces verbose boilerplate without losing clarity
Avoid one-liners when:
- They require a comment to explain what they do
- They chain 4+ operations that are hard to debug
- A named function would communicate intent better
Practice Challenge
Try combining what you learned. Write a function that takes an array of objects, removes duplicates by a specific key, shuffles the result, and returns the last 3 items, all in as few lines as possible.
The best way to internalize these patterns is to use them in your next project today.