Sep 9, 2021 JavaScript
How to unescape html special characters in JavaScript

In this Article we will go through how to unescape html special characters 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 unescape = str => str.replace(/&amp;/g , '&').replace(/&lt;/g  , '<').replace(/&gt;/g  , '>').replace(/&#0*39;/g , "'").replace(/&quot;/g, '"');

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 convert a string to url slug in JavaScript

In this Article we will go through how to convert a string to url slug 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 slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');

Sep 9, 2021 JavaScript
How to count the number of words in a string in JavaScript

In this Article we will go through how to count the number of words in 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 countWords = str => str.trim().split(/\s+/).length;

Sep 9, 2021 JavaScript
How to count the occurrences of a character in a string in JavaScript

In this Article we will go through how to count the occurrences of a character in 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 countOccurrences = (str, char) => [...str].reduce((a, v) => (v === char ? a + 1 : a), 0);

Sep 9, 2021 JavaScript
How to prepend a line number to each line of a text document in JavaScript

In this Article we will go through how to prepend a line number to each line of a text document 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 prependNumbers = str => str.split(/\r?\n/).map((line, i) => `${(i + 1).toString().padStart(2, ' ')} ${line}`).join('\n');

Sep 9, 2021 JavaScript
How to replace the first given number of characters of a string with another character in JavaScript

In this Article we will go through how to replace the first given number of characters of a string with another character 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 mask = (str, num, mask) => `${str}`.slice(num).padStart(`${str}`.length, mask);

Sep 9, 2021 JavaScript
How to sort lines of a text document in the alphabetical order in JavaScript

In this Article we will go through how to sort lines of a text document in the alphabetical order 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 sortLines = str => str.split(/\r?\n/).sort().join('\n');

// Reverse the order
const reverseSortedLines = str => str.split(/\r?\n/).sort().reverse().join('\n');

Sep 9, 2021 JavaScript
How to sort the characters of a string in the alphabetical order in JavaScript

In this Article we will go through how to sort the characters of a string in the alphabetical order 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 sort = str => str.split('').sort((a, b) => a.localeCompare(b)).join('');