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)

Free Resources