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 average of arguments in JavaScript

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

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 emulate a dice throw in JavaScript

In this Article we will go through how to emulate a dice throw 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 throwdice = () => ~~(Math.random() * 6) + 1;

Sep 9, 2021 JavaScript
How to encode a url in JavaScript

In this Article we will go through how to encode a url 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 encode = url => encodeURIComponent(url).replace(/!/g, '%21').replace(/~/g, '%7E').replace(/\*/g, '%2A').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/%20/g, '+');

Sep 9, 2021 JavaScript
How to generate an unique and increment id in JavaScript

In this Article we will go through how to generate an unique and increment id 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 uid = (() => (id = 0, () => id++))();

Sep 9, 2021 JavaScript
How to get the first defined and non null argument in JavaScript

In this Article we will go through how to get the first defined and non null argument 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 coalesce = (...args) => args.find(item => item !== undefined && item !== null);

Sep 9, 2021 JavaScript
How to get the value of a cookie in JavaScript

In this Article we will go through how to get the value of a cookie 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 cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();

Sep 9, 2021 JavaScript
How to get the value of a param from a url in JavaScript

In this Article we will go through how to get the value of a param from a url 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 getParam = (url, param) => new URLSearchParams(new URL(url).search).get(param);