Sep 9, 2021 JavaScript
How to calculate the distance between two points in JavaScript

In this Article we will go through how to calculate the distance between two points 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 distance = (p1, p2) => Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));

Sep 9, 2021 JavaScript
How to check if a point is inside a rectangle in JavaScript

In this Article we will go through how to check if a point is inside a rectangle 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 isInside = (point, rect) => point.x > rect.left && point.x < rect.right && point.y > rect.top && point.y < rect.bottom;

Sep 9, 2021 JavaScript
How to add an ordinal suffix to a number in JavaScript

In this Article we will go through how to add an ordinal suffix to a number 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 addOrdinal = n => `${n}${['st', 'nd', 'rd'][((n + 90) % 100 - 10) % 10 - 1] || 'th'}`;

Sep 9, 2021 JavaScript
How to calculate fibonacci numbers in JavaScript

In this Article we will go through how to calculate fibonacci 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 fibo = (n, memo = {}) => memo[n] || (n <= 2 ? 1 : (memo[n] = fibo(n - 1, memo) + fibo(n - 2, memo)));

Sep 9, 2021 JavaScript
How to calculate the division of arguments in JavaScript

In this Article we will go through how to calculate the division of arguments 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 division = (...args) => args.reduce((a, b) => a / b);

Sep 9, 2021 JavaScript
How to calculate the factorial of a number in JavaScript

In this Article we will go through how to calculate the factorial of a number 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 factorial = n => n <= 1 ? 1 : n * factorial(n - 1);

Sep 9, 2021 JavaScript
How to calculate the remainder of division of arguments in JavaScript

In this Article we will go through how to calculate the remainder of division of arguments 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 remainder = (...args) => args.reduce((a, b) => a % b);

Sep 9, 2021 JavaScript
How to clamp a number between two values in JavaScript

In this Article we will go through how to clamp a number between two 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 clamp = (val, min = 0, max = 1) => Math.max(min, Math.min(max, val));

Sep 9, 2021 JavaScript
How to convert a number to equivalent characters in JavaScript

In this Article we will go through how to convert a number to equivalent characters 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 toChars = n => `${n >= 26 ? toChars(Math.floor(n / 26) - 1) : ''}${'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[n % 26]}`;

Sep 9, 2021 JavaScript
How to truncate a number to a given number of decimal places without rounding in JavaScript

In this Article we will go through how to truncate a number to a given number of decimal places without rounding 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 toFixed = (n, fixed) => `${n}`.match(new RegExp(`^-?\\d+(?:\.\\d{0,${fixed}})?`))[0];