DEV Community

Alex Bobes
Alex Bobes

Posted on

Optimize Your Coding with These 30 Efficient JavaScript One-Liners

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;
Enter fullscreen mode Exit fullscreen mode

2) Check if an array is empty:

const isEmptyArray = arr => arr.length === 0;
Enter fullscreen mode Exit fullscreen mode

3) Reverse an array:

const reverse = arr => arr.reverse();
Enter fullscreen mode Exit fullscreen mode

4) Get the first element of an array:

const first = arr => arr[0];
Enter fullscreen mode Exit fullscreen mode

5) Get the last element of an array:

const last = arr => arr[arr.length - 1];
Enter fullscreen mode Exit fullscreen mode

6) Get the sum of all elements in an array:

const sum = arr => arr.reduce((acc, cur) => acc + cur, 0);
Enter fullscreen mode Exit fullscreen mode

7) Get the product of all elements in an array:

const product = arr => arr.reduce((acc, cur) => acc * cur, 1);
Enter fullscreen mode Exit fullscreen mode

8) Get the average of all elements in an array:

const average = arr => arr.reduce((acc, cur) => acc + cur, 0) / arr.length;
Enter fullscreen mode Exit fullscreen mode

9) Check if a value is in an array:

const includes = (arr, val) => arr.includes(val);
Enter fullscreen mode Exit fullscreen mode

10) Remove duplicates from an array:

const unique = arr => [...new Set(arr)];
Enter fullscreen mode Exit fullscreen mode

11) Get the difference of two arrays:

const difference = (arr1, arr2) => arr1.filter(x => !arr2.includes(x));
Enter fullscreen mode Exit fullscreen mode

12) Get the intersection of two arrays:

const intersection = (arr1, arr2) => arr1.filter(x => arr2.includes(x));
Enter fullscreen mode Exit fullscreen mode

13) Flatten a nested array:

const flatten = arr => arr.flat();
Enter fullscreen mode Exit fullscreen mode

14) Get the maximum value in an array:

const max = arr => Math.max(...arr);
Enter fullscreen mode Exit fullscreen mode

15) Get the minimum value in an array:

const min = arr => Math.min(...arr);
Enter fullscreen mode Exit fullscreen mode

16) Get the nth largest value in an array:

const nthLargest = (arr, n) => arr.sort((a, b) => b - a)[n - 1];
Enter fullscreen mode Exit fullscreen mode

17) Get the nth smallest value in an array:

const nthSmallest = (arr, n) => arr.sort((a, b) => a - b)[n - 1];
Enter fullscreen mode Exit fullscreen mode

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;
}, {});
Enter fullscreen mode Exit fullscreen mode

19) Get the distinct elements in an array:

const distinct = arr => [...new Set(arr)];
Enter fullscreen mode Exit fullscreen mode

20) Check if an array is sorted in ascending order:

const isSortedAscending = arr => arr.every((val, i , cur) => !i || val >= arr[i - 1]);
Enter fullscreen mode Exit fullscreen mode

21) Check if an array is sorted in descending order:

const isSortedDescending = arr => arr.every((val, i, cur) => !i || val <= arr[i - 1]);
Enter fullscreen mode Exit fullscreen mode

22) Remove falsy values from an array:

const compact = arr => arr.filter(Boolean);
Enter fullscreen mode Exit fullscreen mode

23) Get the first n elements of an array:

const firstN = (arr, n) => arr.slice(0, n);
Enter fullscreen mode Exit fullscreen mode

24) Get the last n elements of an array:

const lastN = (arr, n) => arr.slice(arr.length - n, arr.length);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
};
Enter fullscreen mode Exit fullscreen mode

28) Remove the first element of an array:

const removeLast = arr => arr.slice(0, arr.length - 1);
Enter fullscreen mode Exit fullscreen mode

29) Remove the last element of an array:

const removeLast = arr => arr.slice(0, arr.length - 1);
Enter fullscreen mode Exit fullscreen mode

30) Check if an array is a palindrome:

const isPalindrome = arr => arr.toString() === arr.reverse().toString();
Enter fullscreen mode Exit fullscreen mode

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.

Oldest comments (0)