Sep 9, 2021 JavaScript
How to get the current quarter of a date in JavaScript

In this Article we will go through how to get the current quarter of a date 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 getQuarter = (d = new Date()) => Math.ceil((d.getMonth() + 1) / 3);

Sep 9, 2021 JavaScript
How to sort an array of dates in JavaScript

In this Article we will go through how to sort an array of dates 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 sortDescending = arr => arr.sort((a, b) => a.getTime() > b.getTime());
const sortAscending = arr => arr.sort((a, b) => a.getTime() < b.getTime());

Sep 9, 2021 JavaScript
How to check if the code is running in the browser in JavaScript

In this Article we will go through how to check if the code is running in the browser only using single line of code in JavaScript.

Let's define this short function:

const isBrowser = typeof window === 'object' && typeof document === 'object';

Sep 9, 2021 JavaScript
How to clear all cookies in JavaScript

In this Article we will go through how to clear all cookies 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 clearCookies = () => document.cookie.split(';').forEach(c => document.cookie = c.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date().toUTCString()};path=/`));

Sep 9, 2021 JavaScript
How to convert celsius to fahrenheit in JavaScript

In this Article we will go through how to convert celsius to fahrenheit 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 celsiusToFahrenheit = celsius => celsius * 9/5 + 32;

Sep 9, 2021 JavaScript
How to convert cookie to object in JavaScript

In this Article we will go through how to convert cookie 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 cookies = document.cookie.split(';').map(item => item.split('=')).reduce((acc, [k, v]) => (acc[k.trim().replace('"', '')] = v) && acc, {});

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 check if the code is running in nodejs in JavaScript

In this Article we will go through how to check if the code is running in nodejs only using single line of code in JavaScript.

Let's define this short function:

const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;

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}), {});