Sep 9, 2021 JavaScript
How to create cartesian product in JavaScript

In this Article we will go through how to create cartesian product 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 cartesian = (...sets) => sets.reduce((acc, set) => acc.flatMap((x) => set.map((y) => [...x, y])), [[]]);

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

In this Article we will go through how to empty 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 empty = arr => arr.length = 0;

Sep 9, 2021 JavaScript
How to get indices of a value in an array in JavaScript

In this Article we will go through how to get indices 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 indices = (arr, value) => arr.reduce((acc, v, i) => (v === value ? [...acc, i] : acc), []);

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

In this Article we will go through how to get the average 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 average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;

Sep 9, 2021 JavaScript
How to check if a date is today in JavaScript

In this Article we will go through how to check if a date is today 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 isToday = (date) => date.toISOString().slice(0, 10) === new Date().toISOString().slice(0, 10);

Sep 9, 2021 JavaScript
How to check if a flat array has duplicate values in JavaScript

In this Article we will go through how to check if a flat array has duplicate values 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 hasDuplicateValues = arr => new Set(arr).size !== arr.length;

Sep 9, 2021 JavaScript
How to check if a number is even in JavaScript

In this Article we will go through how to check if a number is even 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 isEven = number => number % 2 === 0;

Sep 9, 2021 JavaScript
How to check if a number is in a given range in JavaScript

In this Article we will go through how to check if a number is in a 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 inRange = (num, a, b, threshold = 0) => (Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold);

Sep 9, 2021 JavaScript
How to check if a number is odd in JavaScript

In this Article we will go through how to check if a number is odd 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 isOdd = number => number % 2 !== 0;

Sep 9, 2021 JavaScript
How to check if a string contains only letters and numbers in JavaScript

In this Article we will go through how to check if a string contains only letters and 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 isAlphanumeric = str => /^[0-9A-Z]+$/i.test(str);