Sep 9, 2021 JavaScript
How to check if an array is empty in JavaScript

In this Article we will go through how to check if an array is empty only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const isEmpty = arr => !Array.isArray(arr) || arr.length === 0;

Sep 9, 2021 JavaScript
How to compare two arrays in JavaScript

In this Article we will go through how to compare two arrays only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);

Sep 9, 2021 JavaScript
How to count the occurrences of a value in an array in JavaScript

In this Article we will go through how to count the occurrences of a value in an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

Sep 9, 2021 JavaScript
How to create an array of numbers in the given range in JavaScript

In this Article we will go through how to create an array of numbers in the given range only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const range = (min, max) => [...Array(max - min + 1).keys()].map(i => i + min);

Sep 9, 2021 JavaScript
How to find the maximum item of an array by given key in JavaScript

In this Article we will go through how to find the maximum item of an array by given key only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const maxBy = (arr, key) => arr.reduce((a, b) => a[key] >= b[key] ? a : b, {});

Sep 9, 2021 JavaScript
How to get all subsets of an array in JavaScript

In this Article we will go through how to get all subsets of an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);

Sep 9, 2021 JavaScript
How to get the sum of an array of numbers in JavaScript

In this Article we will go through how to get the sum of an array of numbers only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const sum = arr => arr.reduce((a, b) => a + b, 0);

Sep 9, 2021 JavaScript
How to remove falsy values from array in JavaScript

In this Article we will go through how to remove falsy values from array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const removeFalsy = arr => arr.filter(Boolean);

Sep 9, 2021 JavaScript
How to shuffle an array in JavaScript

In this Article we will go through how to shuffle an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const shuffle = arr => arr.map(a => ({ sort: Math.random(), value: a })).sort((a, b) => a.sort - b.sort).map(a => a.value);

Sep 9, 2021 JavaScript
How to clone an array in JavaScript

In this Article we will go through how to clone an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const clone = arr => arr.slice(0);