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


π 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()
andrepeat()
doHow 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
Method | Purpose | Returns | Modifies Original? |
replace() | Replace part of a string | New string | β No |
repeat() | Repeat a string multiple times | New string | β No |
π‘ Real-Life Use Cases
π Replacing text in user inputs or messages
π§Ό Sanitizing data
β¨ Creating loading animations like
"."
,".."
,"..."
withrepeat()
π 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!
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
