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 mod of collection index in JavaScript

In this Article we will go through how to calculate the mod of collection index 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 mod = (a, b) => ((a % b) + b) % b;

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);

Sep 9, 2021 JavaScript
How to get type of a variable in string in JavaScript

In this Article we will go through how to get type of a variable in string 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 getTypeOf = obj => Object.prototype.toString.call(obj).match(/\[object (.*)\]/)[1];

Sep 9, 2021 JavaScript
How to redirect the page to https if it is in http in JavaScript

In this Article we will go through how to redirect the page to https if it is in http 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 redirectHttps = () => (location.protocol === 'https:') ? {} : location.replace(`https://${location.href.split('//')[1]}`);

Sep 9, 2021 JavaScript
How to run promises in sequence in JavaScript

In this Article we will go through how to run promises in sequence 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 run = promises => promises.reduce((p, c) => p.then(rp => c.then(rc => [...rp, rc])), Promise.resolve([]));

Sep 9, 2021 JavaScript
How to swap two variables in JavaScript

In this Article we will go through how to swap two variables 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:

[a, b] = [b, a];

Sep 9, 2021 JavaScript
How to convert url parameters to object in JavaScript

In this Article we will go through how to convert url parameters to object 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 getUrlParams = query => Array.from(new URLSearchParams(query)).reduce((p, [k, v]) => Object.assign({}, p, { [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v}), {});