Sep 9, 2021 JavaScript
How to capitalize a string in JavaScript

In this Article we will go through how to capitalize a 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 capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;

Sep 9, 2021 JavaScript
How to check if a path is relative in JavaScript

In this Article we will go through how to check if a path is relative 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 isRelative = path => !/^([a-z]+:)?[\\/]/i.test(path);

Sep 9, 2021 JavaScript
How to check if a string consists of a repeated character sequence in JavaScript

In this Article we will go through how to check if a string consists of a repeated character 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 consistsRepeatedSubstring = str => `${str}${str}`.indexOf(str, 1) !== str.length;

Sep 9, 2021 JavaScript
How to check if a string is a palindrome in JavaScript

In this Article we will go through how to check if a string is a palindrome 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 isPalindrome = str => str === str.split('').reverse().join('');

Sep 9, 2021 JavaScript
How to check if a url is absolute in JavaScript

In this Article we will go through how to check if a url is absolute 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 isAbsoluteUrl = url => /^[a-z][a-z0-9+.-]*:/.test(url);

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