Understanding JavaScript's replace() & repeat() Methods: Practical Examples Included

Anmol singhAnmol singh
3 min read

πŸ” JavaScript replace() & repeat() Methods Explained with Real Examples

If you're learning JavaScript, working with strings is unavoidable. Luckily, JavaScript offers built-in methods to make string manipulation easierβ€”two of them are replace() and repeat().

In this article, we’ll explore:

  • What replace() and repeat() do

  • How to use them with real examples

  • Common use cases for beginners and frontend developers


πŸ”„ replace() Method in JavaScript

The replace() method searches for a specified value within a string and replaces it with another value. It returns a new string and does not change the original one.

πŸ“Œ Syntax

jsCopyEditstring.replace(searchValue, newValue)

🧠 How It Works:

  • It searches for the first occurrence of searchValue.

  • When found, it replaces that with newValue.

  • Only the first match is replaced unless you're using regex with the global (g) flag.


βœ… Example 1: Replace a Word

Let’s say we have this string:

jsCopyEditlet str = "ilovecoding";
console.log(str.replace("love", "do"));
// Output: "idocoding"

🧠 Explanation:
It finds the word "love" and replaces it with "do".


βœ… Example 2: Replace a Character

jsCopyEditlet str = "ilovecoding";
console.log(str.replace("e", "x"));
// Output: "ilovxcoding"

It only replaces the first occurrence of e.


❌ Common Mistake: Replacing a Non-Existent Character

jsCopyEditlet str = "ilovecoding";
console.log(str.replace("0", "X"));
// Output: "ilovecoding"

🧠 "0" (zero) does not exist in the string, so the result remains unchanged.


βœ… Practical Example

jsCopyEditlet msg = "ilovecoding";
console.log(msg.replace("love", "do"));
// Output: "idocoding"

Great for customizing or sanitizing text content dynamically in a web app.


πŸ” repeat() Method in JavaScript

The repeat() method returns a new string with a specified number of copies of the original string.

πŸ“Œ Syntax

jsCopyEditstring.repeat(count)
  • count: An integer >= 0. If it's a decimal, it's rounded down.

  • Throws an error if negative or infinite.


βœ… Example 1: Repeating a Word

jsCopyEditlet str = "mango";
console.log(str.repeat(3));
// Output: "mangomangomango"

Useful when generating repeated patterns, placeholders, or animations.


❗ Edge Cases to Know:

jsCopyEditlet word = "test";
console.log(word.repeat(0)); // Output: ""

Zero repetition returns an empty string.


βœ… Recap: Quick Comparison

MethodPurposeReturnsModifies Original?
replace()Replace part of a stringNew string❌ No
repeat()Repeat a string multiple timesNew string❌ No

πŸ’‘ Real-Life Use Cases

  • πŸ”„ Replacing text in user inputs or messages

  • 🧼 Sanitizing data

  • ✨ Creating loading animations like ".", "..", "..." with repeat()

  • πŸ“Š Replacing values in templates or reports


πŸ“£ Call to Action

Was this helpful?

πŸ‘‰ Share it with your fellow JavaScript learners.
πŸ’¬ Got questions or a cool use case of replace() or repeat()? Drop it in the comments.
πŸ“Œ Follow me on Hashnode for more JavaScript deep dives!

0
Subscribe to my newsletter

Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anmol singh
Anmol singh