TypeError: replace is not a function in JavaScript

TypeError: .replace is not a function occurs when we call replace() function on object which is not an string. replace() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling replace() method.

Let’s see with help of simple example where we want to remove . from number.

For example: 1.23 should become 123.

You will get following error when you execute the program:

We got this error because num is not an string, it is number.

Convert num to string using toString() method and it will work fine.

Output:

You can also check if it is type of string before calling replace() to make sure that replace() is being called on string only.

Output:

As you can see num is not a string, so result is empty string.

We used ternary operator to check if num is string or not. If it is a string, call the replace method, otherwise return empty string.

Read also: map is not a function in JavaScript.

That’s all about how to resolve TypeError: replace is not a function in JavaScript.

Was this post helpful?

Related Posts

  • 25 May

    Remove Double Quotes from String in JavaScript

    Table of ContentsWays to Remove Double Quotes from String in JavaScriptUsing replace()Using replaceAll()Using split() and join() methodUsing for loopConclusionWas this post helpful? In this post, we will see how to remove double quotes from String in JavaScript. Ways to Remove Double Quotes from String in JavaScript There are multiple ways to do it. Let’s go […]

  • 25 May

    Convert Seconds to Hours Minutes Seconds in Javascript

    Table of ContentsUsing javascript without any external libraryUsing momentJs libraryWas this post helpful? In this post, we will see how to convert seconds to Hours Minutes Seconds in JavaScript. There are multiple ways to do it. Let’s go through them. Using javascript without any external library To convert seconds to Hours Minutes Seconds in javascript: […]

  • 25 May

    Remove First Character from String in JavaScript

    Table of ContentsWays to Remove First Character from string in JavaScriptUsing substring()Using slice()Using substr()Using replace() methodUsing Array’s shift() with split() and join()ConclusionWas this post helpful? In this post, we will see how to remove first character from string in JavaScript. Since Strings are immutable in JavaScript, you can’t do it in-place. All the methods will […]

  • 25 May

    [Solved] TypeError: toLowerCase is not a function in JavaScript

    TypeError: .toLowerCase is not a function occurs when we call toLowerCase() function on object which is not an string. toLowerCase() function can be only called on string. To resolve this issue, convert value to string using toString() ,method before calling toLowerCase() method. Let’s see with help of simple example [crayon-62b8c02cd507b480146418/] You will get following error […]

  • 25 May

    Escape Json String Containing Newline Characters in JavaScript

    Table of ContentsEscape JSON string in JavaScriptConclusionWas this post helpful? In this post, we will see how to escape JSON string containing newline characters using JavaScript. There is no well known JavaScript library which can escape all special characters in String. Escape JSON string in JavaScript There is no direct way to escape JSON String […]

  • 23 May

    Escape Apostrophe in JavaScript

    Table of ContentsWays to escape Apostrophe in JavaScriptUsing escape characterUsing alternate String syntaxUsing template literalsWas this post helpful? In this article, we will see how to escape Apostrophe in JavaScript. Let’s see what happens if we have singe quote in String and we also use single quotes to declare the string in JavaScript. [crayon-62b8c02cd7244889790420/] Output: […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.