What is replace() in JavaScript?

The replace() method in JavaScript replaces a specified string with a given string.

Syntax

The syntax of this method is as follows:


str.replace(oldString, newString);

Parameters

The replace() method takes two mandatory string/character parameters. The first string is the one that needs to be replaced. The second string is the one that needs to be placed.

Return value

The return value is the updated string with the replaced bits in it.

Code

In the code below, the replace() method changes the string by replacing the old string with a new one.

// replacing "Windows" with ""Educative
let string = "Welcome to Windows";
let x = string.replace("Windows", "Educative");
console.log(x)
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources