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 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 3 digits color to 6 digits color in JavaScript

In this Article we will go through how to convert 3 digits color to 6 digits color 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 toFullHexColor = color => `#${(color.startsWith('#') ? color.slice(1) : color).split('').map(c => `${c}${c}`).join('')}`;

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 degrees to radians in JavaScript

In this Article we will go through how to convert degrees to radians 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 degsToRads = deg => deg * Math.PI / 180.0;

Sep 9, 2021 JavaScript
How to convert radians to degrees in JavaScript

In this Article we will go through how to convert radians to degrees 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 radsToDegs = rad => rad * 180 / Math.PI;

Sep 9, 2021 JavaScript
How to normalize the ratio of a number in a range in JavaScript

In this Article we will go through how to normalize the ratio of a number in a 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 normalizeRatio = (value, min, max) => (value - min) / (max - min);

Sep 9, 2021 JavaScript
How to round a number to the nearest multiple of a given value in JavaScript

In this Article we will go through how to round a number to the nearest multiple of a given value 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 roundNearest = (value, nearest) => Math.round(value / nearest) * nearest;

Sep 9, 2021 JavaScript
How to calculate the linear interpolation between two numbers in JavaScript

In this Article we will go through how to calculate the linear interpolation between two 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 lerp = (a, b, amount) => (1 - amount) * a + amount * b;