Hey fellow developers!
In this article, I will share with you 30 powerful one-liner code snippets in JavaScript that can help you optimize your coding and write more efficient, cleaner code. These one-liners are designed to help you accomplish common tasks with less code, making your development process faster, easier, and more enjoyable.
Without further ado, let's dive into the code.
1) Get the length of an array:
const length = arr => arr.length;
2) Check if an array is empty:
const isEmptyArray = arr => arr.length === 0;
3) Reverse an array:
const reverse = arr => arr.reverse();
4) Get the first element of an array:
const first = arr => arr[0];
5) Get the last element of an array:
const last = arr => arr[arr.length - 1];
6) Get the sum of all elements in an array:
const sum = arr => arr.reduce((acc, cur) => acc + cur, 0);
7) Get the product of all elements in an array:
const product = arr => arr.reduce((acc, cur) => acc * cur, 1);
8) Get the average of all elements in an array:
const average = arr => arr.reduce((acc, cur) => acc + cur, 0) / arr.length;
9) Check if a value is in an array:
const includes = (arr, val) => arr.includes(val);
10) Remove duplicates from an array:
const unique = arr => [...new Set(arr)];
11) Get the difference of two arrays:
const difference = (arr1, arr2) => arr1.filter(x => !arr2.includes(x));
12) Get the intersection of two arrays:
const intersection = (arr1, arr2) => arr1.filter(x => arr2.includes(x));
13) Flatten a nested array:
const flatten = arr => arr.flat();
14) Get the maximum value in an array:
const max = arr => Math.max(...arr);
15) Get the minimum value in an array:
const min = arr => Math.min(...arr);
16) Get the nth largest value in an array:
const nthLargest = (arr, n) => arr.sort((a, b) => b - a)[n - 1];
17) Get the nth smallest value in an array:
const nthSmallest = (arr, n) => arr.sort((a, b) => a - b)[n - 1];
18) Get the frequency of each element in an array:
const frequency = arr => arr.reduce((acc, cur) => {
acc[cur] = acc[cur] ? acc[cur] + 1 : 1;
return acc;
}, {});
19) Get the distinct elements in an array:
const distinct = arr => [...new Set(arr)];
20) Check if an array is sorted in ascending order:
const isSortedAscending = arr => arr.every((val, i , cur) => !i || val >= arr[i - 1]);
21) Check if an array is sorted in descending order:
const isSortedDescending = arr => arr.every((val, i, cur) => !i || val <= arr[i - 1]);
22) Remove falsy values from an array:
const compact = arr => arr.filter(Boolean);
23) Get the first n elements of an array:
const firstN = (arr, n) => arr.slice(0, n);
24) Get the last n elements of an array:
const lastN = (arr, n) => arr.slice(arr.length - n, arr.length);
25) Get the sum of all even numbers in an array:
const sumOfEven = arr => arr.filter(x => x % 2 === 0).reduce((acc, cur) => acc + cur, 0);
26) Get the sum of all odd numbers in an array:
const sumOfOdd = arr => arr.filter(x => x % 2 !== 0).reduce((acc, cur) => acc + cur, 0);
27) Get the elements that occur n times in an array:
const nOccurrences = (arr, n) => {
const frequency = arr.reduce((acc, cur) => {
acc[cur] = acc[cur] ? acc[cur] + 1 : 1;
return acc;
}, {});
return Object.keys(frequency).filter(x => frequency[x] === n);
};
28) Remove the first element of an array:
const removeLast = arr => arr.slice(0, arr.length - 1);
29) Remove the last element of an array:
const removeLast = arr => arr.slice(0, arr.length - 1);
30) Check if an array is a palindrome:
const isPalindrome = arr => arr.toString() === arr.reverse().toString();
And that's just the tip of the iceberg. These one-liners are versatile and can be used in a variety of situations to streamline your code.
It's worth mentioning that some of these one-liners might have been posted or published before in various sources, but the purpose of this post is to bring together a comprehensive collection of useful and efficient JavaScript one-liners that can help developers optimize their coding.
Top comments (0)