Table of Contents
Reverse a String
const reverseString = str => str.split('').reverse().join('');
Explanation: This one-liner splits a string into an array of characters, reverses the array, and joins it back into a string.
Keywords: JavaScript string manipulation, reverse string in JavaScript, JavaScript one-liner.
Check if a Number is Even
const isEven = num => num % 2 === 0;

Explanation: This checks if a number is even by using the modulus operator. If the result is 0, the number is even.
Keywords: check even number JavaScript, modulus operator in JavaScript.
Flatten a Multidimensional Array
const flattenArray = arr => arr.flat(Infinity);
Explanation: This one-liner flattens an array of arrays into a single-level array using the flat()
method with a depth of Infinity
.
Keywords: flatten JavaScript array, multidimensional array flattening.
Remove Duplicates from an Array
const removeDuplicates = arr => [...new Set(arr)];
Explanation: This uses the Set
object to eliminate duplicate values in an array, then spreads the unique values back into an array.
Keywords: remove array duplicates JavaScript, JavaScript Set method.
Generate a Random Number Between Two Values
const randomNum = (min, max) => Math.random() * (max - min) + min;
Explanation: This generates a random number between the min
and max
values, offering more flexibility in random number generation.
Keywords: random number generation JavaScript, JavaScript Math.random.
Capitalize the First Letter of a String
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
Explanation: This one-liner capitalizes the first letter of a string by using charAt()
to target the first character and toUpperCase()
to capitalize it.
Keywords: capitalize string JavaScript, first letter uppercase JavaScript.
Calculate the Factorial of a Number
const factorial = n => n === 0 ? 1 : n * factorial(n - 1);
Explanation: This recursive one-liner calculates the factorial of a given number n
.
Keywords: JavaScript factorial calculation, recursive functions JavaScript.
Check if a String is a Palindrome
const isPalindrome = str => str === str.split('').reverse().join('');
Explanation: This checks whether a string is the same forward and backward by reversing the string and comparing it to the original.
Keywords: palindrome check JavaScript, reverse string JavaScript.
Convert RGB to Hex
const rgbToHex = (r, g, b) => `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)}`;
Explanation: This one-liner converts RGB color values to a hexadecimal format.
Keywords: RGB to Hex JavaScript, color conversion JavaScript.
Get the Last Element of an Array
const lastElement = arr => arr[arr.length - 1];
Explanation: This retrieves the last element from an array using its length property.
Keywords: get last array element JavaScript, array manipulation JavaScript.
Sum All Numbers in an Array
const sumArray = arr => arr.reduce((acc, num) => acc + num, 0);
Explanation: This one-liner uses the reduce()
method to sum all the numbers in an array.
Keywords: sum array elements JavaScript, reduce method JavaScript.
Convert Fahrenheit to Celsius
const fahrenheitToCelsius = f => (f - 32) * 5 / 9;
Explanation: This converts a temperature from Fahrenheit to Celsius using the standard formula.
Keywords: Fahrenheit to Celsius conversion JavaScript, temperature conversion JavaScript.
Generate a Random Hex Color
const randomHexColor = () => `#${Math.floor(Math.random() * 16777215).toString(16)}`;
Explanation: This generates a random hex color code by generating a random number and converting it to hexadecimal.
Keywords: random color JavaScript, hex color generation.
Shuffle an Array
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
Explanation: This uses the sort()
method with Math.random()
to shuffle an array randomly.
Keywords: shuffle array JavaScript, random sort JavaScript.
Convert a String to an Array of Words
const stringToWords = str => str.trim().split(/\s+/);
Explanation: This one-liner splits a string into an array of words, trimming extra spaces and splitting by whitespace.
Keywords: split string into words JavaScript, string manipulation JavaScript.
Generate a UUID
const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => (c === 'x' ? Math.random() * 16 | 0 : 'r').toString(16));
Explanation: This generates a random UUID (Universally Unique Identifier) using Math.random()
.
Keywords: generate UUID JavaScript, random UUID generator.
Check if an Array is Empty
const isEmptyArray = arr => !Array.isArray(arr) || !arr.length;
Explanation: This checks if an array is empty by first verifying it’s an array, then checking the length.
Keywords: check empty array JavaScript, array validation JavaScript.
Get a Random Array Element
const randomElement = arr => arr[Math.floor(Math.random() * arr.length)];
Explanation: This selects a random element from an array using Math.random()
and Math.floor()
.
Keywords: get random array element JavaScript, array randomizer JavaScript.
Conclusion: Mastering JavaScript One-Liners
JavaScript one-liners offer a powerful way to streamline code, enhance performance, and demonstrate proficiency in the language. By mastering these 18 pro-level one-liners, you’ll improve the efficiency and elegance of your code. Whether you’re manipulating arrays, handling strings, or calculating values, these one-liners will make you a more productive and skilled developer.
For more JavaScript tips and tutorials, stay tuned to our blog and continue expanding your coding expertise!