FrontendDeveloper.in

ECMAScript question detail

replaceAll

The new replaceAll() method from String prototype is used to replace all the occurrences of a string from another string value. Earlier it was not possible to replace all the instances of a substring without the use of regex.

Before ES2021

console.log('10101010'.replace(new RegExp('0', 'g'), '1')); // 11111111
console.log('01010101'.replace(/0/g, '1')); // 11111111

After ES2021

console.log('10101010'.replaceAll('0', '1')); // 11111111
console.log('01010101'.replaceAll('0', '1')); // 11111111
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399