Sep 9, 2021 JavaScript
How to remove empty lines of a text document in JavaScript

In this Article we will go through how to remove empty lines 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 removeEmptyLines = str => str.split(/\r?\n/).filter(line => line.trim() !== '').join('\n');

Sep 9, 2021 JavaScript
How to remove spaces from a string in JavaScript

In this Article we will go through how to remove spaces from 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 removeSpaces = str => str.replace(/\s/g, '');

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

In this Article we will go through how to repeat 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 repeat = (str, numberOfTimes) => str.repeat(numberOfTimes);

Sep 9, 2021 JavaScript
How to replace all line breaks with br elements in JavaScript

In this Article we will go through how to replace all line breaks with br elements 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 nl2br = str => str.replace(new RegExp('\r?\n', 'g'), '<br>');

// In React
str.split('\n').map((item, index) => <React.Fragment key={index}>{item}<br /></React.Fragment>)

Sep 9, 2021 JavaScript
How to replace all tab characters with spaces in JavaScript

In this Article we will go through how to replace all tab characters with spaces 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 replace = (str, numSpaces = 4) => str.replaceAll('\t', ' '.repeat(numSpaces));

Sep 9, 2021 JavaScript
How to replace multiple spaces with a single space in JavaScript

In this Article we will go through how to replace multiple spaces with a single space 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:

// Replace spaces, tabs and new line characters
const replaceSpaces = str => str.replace(/\s\s+/g, ' ');

// Only replace spaces
const replaceOnlySpaces = str => str.replace(/  +/g, ' ');

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 reverse a string in JavaScript

In this Article we will go through how to reverse 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 reverse = str => str.split('').reverse().join('');

Sep 9, 2021 JavaScript
How to invert keys and values of an object in JavaScript

In this Article we will go through how to invert keys and values of an 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 invert = obj => Object.keys(obj).reduce((res, k) => Object.assign(res, {[obj[k]]: k}), {});

Sep 9, 2021 JavaScript
How to omit a subset of properties from an object in JavaScript

In this Article we will go through how to omit a subset of properties from an 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 omit = (obj, keys) => Object.keys(obj).filter(k => !keys.includes(k)).reduce((res, k) => Object.assign(res, {[k]: obj[k]}), {});