Replace text in string JavaScript

Published on September 4th, 2019

javascript icon

Today I learned that JavaScript has replace method. It’s fairly simple and gets the job done.

For example, you have string similar to this one.

const favoriteFood = 'My favorite food is Pizza'

And suddenly broccoli becomes your favorite food. All you need to do is run replace method on string, as first parameter pass text you want to replace (it can be regex too) and as second parameter pass new text.

const newFavoriteFood = favoriteFood.replace('Pizza', 'Broccoli')

That’s it. Can’t get much easier than this.