Sep 9, 2021 JavaScript
How to detect dark mode in JavaScript

In this Article we will go through how to detect dark mode only using single line of code in JavaScript.

Let's define this short function:

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

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 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 wait for an amount of time in JavaScript

In this Article we will go through how to wait for an amount of time 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 wait = async (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));

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 fahrenheit to celsius in JavaScript

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