How to replace a string in JavaScript

The replace() method searches a string for a specified substring (of a regular expression) and replaces the specified substring with the new string. ​

svg viewer

Syntax

Below is the syntax for the replace() function in JavaScript:

svg viewer

Code

The code snippet below illustrates the usage of the replace() function in JavaScript:

// original string
var str = "I am working in Python"
console.log(str)
// replace function
str = str.replace("Python","JavaScript")
console.log(str)
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

Copyright ©2025 Educative, Inc. All rights reserved