Top 10 JavaScript One-Liners You Should Know

JavaScript is packed with clever tricks that can save you time and make your code more readable. Sometimes all you need is one good line to solve a common problem. In this article, we’ll go through 10 JavaScript one-liners that are useful, elegant, and worth memorizing.
Some of these are practical in production code. Others are great for interviews, quick utilities, or showing off how well you know the language. Let’s dive in.
1. Swap Two Variables Without a Temp Variable
[a, b] = [b, a]
This uses array destructuring to swap values. No need for a third variable.
let a = 5
let b = 10
[a, b] = [b, a]
// a is now 10, b is now 5
2. Generate an Array of Numbers
[...Array(10).keys()]
This creates an array with numbers from 0 to 9.
const nums = [...Array(10).keys()]
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
If you want to start from 1:
[...Array(10).keys()].map(i => i + 1)
3. Check If a Value Is an Integer
Number.isInteger(val)
Clean and built-in way to check if a value is a whole number.
Number.isInteger(4) // true
Number.isInteger(4.5) // false
4. Remove Duplicates from an Array
[...new Set(arr)]
Using a Set automatically removes duplicate values.
const numbers = [1, 2, 2, 3, 3, 3, 4]
const unique = [...new Set(numbers)]
// [1, 2, 3, 4]
5. Get a Random Item from an Array
arr[Math.floor(Math.random() * arr.length)]
This is a quick way to select one element at random.
const colors = ['red', 'green', 'blue']
const randomColor = colors[Math.floor(Math.random() * colors.length)]
6. Reverse a String
str.split('').reverse().join('')
There’s no built-in reverse()
method for strings, but this one-liner does the job.
const reversed = 'hello'.split('').reverse().join('')
// 'olleh'
7. Capitalize the First Letter of a Word
str.charAt(0).toUpperCase() + str.slice(1)
This one-liner works for short strings or formatting titles.
const title = 'javascript'
const capitalized = title.charAt(0).toUpperCase() + title.slice(1)
// 'Javascript'
8. Flatten a Nested Array
arr.flat(Infinity)
Useful when you have arrays inside arrays and want one flat list.
const nested = [1, [2, [3, [4]]]]
const flat = nested.flat(Infinity)
// [1, 2, 3, 4]
9. Get the Last Element of an Array
arr.at(-1)
In modern JavaScript, .at()
lets you index from the end using negative numbers.
const list = [10, 20, 30, 40]
const last = list.at(-1)
// 40
Alternatively, the classic version:
arr[arr.length - 1]
10. Count Occurrences in an Array
arr.reduce((acc, val) => ((acc[val] = (acc[val] || 0) + 1), acc), {})
This one-liner gives you a frequency map of items in the array.
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
const count = fruits.reduce((acc, val) => ((acc[val] = (acc[val] || 0) + 1), acc), {})
/*
{
apple: 3,
banana: 2,
orange: 1
}
*/
Bonus: FizzBuzz in One Line
for(let i=1;i<=100;i++)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)
This famous coding challenge can be solved in a single line, though for readability it's usually better to write it in a more expanded way.
When to Use These One-Liners
These one-liners are great tools for your JavaScript toolbox. In real projects, clarity is more important than cleverness. If a one-liner makes the code harder to read, it's perfectly fine to break it into multiple lines.
That said, knowing how these expressions work helps you understand the language better. You’ll find opportunities where these tricks simplify your code, especially in small utilities, data transformations, or interview challenges.
How to Practice These
- Use them in online editors like JSFiddle, CodePen, or StackBlitz
- Try converting basic coding challenges into one-liners
- Write your own utility functions using these techniques
Even if you don’t use them every day, these patterns will make you a more expressive and confident JavaScript developer.