10 Mind-Blowing JavaScript One-Liners
JavaScript is a powerful and expressive language that allows you to write complex functionalities in just a single line of code. Whether youβre a beginner or a seasoned developer, these one-liners will help you write cleaner, more efficient, and impressive JavaScript. Letβs dive into some mind-blowing JavaScript tricks!

1. Reverse a String
const reverseString = str => [...str].reverse().join('');
Usage:
console.log(reverseString("hello")); // "olleh"
This one-liner uses the spread operator ([...]
) to convert the string into an array, reverses it, and then joins it back into a string.
2. Check If a Number Is Even
const isEven = num => num % 2 === 0;
Usage:
console.log(isEven(4)); // true
console.log(isEven(7)); // false
This simple one-liner checks whether a number is even using the modulo (%
) operator.
3. Generate a Random Number Between Two Values
const randomBetween = (min, max) => Math.random() * (max - min) + min;
Usage:
console.log(randomBetween(5, 10)); // e.g., 7.234562
This generates a random floating-point number within a specified range.
4. Shuffle an Array
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
Usage:
console.log(shuffleArray([1, 2, 3, 4, 5]));
This one-liner randomly shuffles an array using sort()
with a random comparison function.
5. Remove Duplicates from an Array
const removeDuplicates = arr => [...new Set(arr)];
Usage:
console.log(removeDuplicates([1, 2, 2, 3, 4, 4, 5])); // [1, 2, 3, 4, 5]
This utilizes Set
, which automatically removes duplicate values from an array.
6. Get the Last Element of an Array
const lastElement = arr => arr[arr.length - 1];
Usage:
console.log(lastElement([10, 20, 30, 40])); // 40
Retrieves the last element by using the array length minus one.
7. Capitalize the First Letter of a String
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
Usage:
console.log(capitalize("hello")); // "Hello"
This one-liner takes the first letter of a string, capitalizes it, and appends the rest of the string.
8. Check if an Object is Empty
const isEmptyObject = obj => Object.keys(obj).length === 0;
Usage:
console.log(isEmptyObject({})); // true
console.log(isEmptyObject({ a: 1 })); // false
By checking the length of the keys array, we can determine if an object is empty.
9. Convert RGB to Hex
const rgbToHex = (r, g, b) => `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}`;
Usage:
console.log(rgbToHex(255, 99, 71)); // "#ff6347"
This one-liner converts RGB color values into a hex code using bitwise operations.
10. Get the Current Timestamp
const timestamp = () => Date.now();
Usage:
console.log(timestamp()); // 1712345678901
This one-liner returns the current timestamp in milliseconds.
Conclusion
JavaScript one-liners can be extremely powerful and save you time when coding. Whether itβs manipulating strings, working with arrays, or performing mathematical calculations, these tricks can make your code more elegant and efficient. Try them out and start coding smarter today!
π₯ Found this blog post helpful? π₯
If you enjoyed this article and found it valuable, please show your support by clapping π and subscribing to my blog for more in-depth insights on web development and Next.js!
Subscribe here: click me
π Follow me on:
π Website: sagarsangwan.vercel.app
π¦ Twitter/X: @sagar sangwan
π LinkedIn: Sagar Sangwan
πΈ Instagram: @codebysagar
Your encouragement helps me continue creating high-quality content that can assist you on your development journey. π
Written by Sagar Sangwan
π¨βπ» Programmer | βοΈ Love Traveling | π³ Enjoy Cooking | Building cool tech and exploring the world!
View more blogs by me CLICK HERE
Loading related blogs...
SUBSCRIBE to Newsletter
In this newsletter we provide latest news about technology, business and startup ideas. Hope you like it.