String Replace in JavaScript

Level 265 keystrokes

replace() returns a new string with the first match of a pattern replaced.

JavaScript code

// Create a string
1let str = "Hello World";
// replace() searches for "World" and replaces it with "JS"
// Returns a new string; original is unchanged
// Result: "Hello JS"
2console.log(str.replace("World", "JS"));

Output

Hello JS
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • "× 6
  • ;× 2
  • .× 2
  • (× 2
  • )× 2
  • ,× 1
  • =× 1

More JavaScript examples at level 2

← JavaScript typing practice