{"id":20056,"date":"2022-05-22T12:44:09","date_gmt":"2022-05-22T07:14:09","guid":{"rendered":"https:\/\/java2blog.com\/?p=20056"},"modified":"2022-05-22T15:19:29","modified_gmt":"2022-05-22T09:49:29","slug":"typeerror-replace-is-not-function-javascript","status":"publish","type":"post","link":"https:\/\/java2blog.com\/typeerror-replace-is-not-function-javascript\/","title":{"rendered":"TypeError: replace is not a function in JavaScript"},"content":{"rendered":"<p><strong><code>TypeError: .replace is not a function<\/code> occurs when we call <code>replace()<\/code> function on object which is not an string. <code>replace()<\/code> function can be only called on string. To resolve this issue, convert value to string using <code>toString()<\/code> ,method before calling replace() method.<\/strong><\/p>\n<p>Let&#8217;s see with help of simple example where we want to remove <code>.<\/code> from number.<\/p>\n<p><strong>For example<\/strong>: 1.23 should become 123.<\/p>\n<pre code=\"javascript\" mark=\"18\" title=\"Calling map on Object rather than array\">\nvar num=1.23;\nnum=num.replace(\/\\.\/g,'');\nconsole.log(num);\n<\/pre>\n<p>You will get following error when you execute the program:<\/p>\n<pre code=\"javascript\" title=\"Output\">\nnum=num.replace(\/\\.\/g,'');\n        ^\n\nTypeError: num.replace is not a function\n    at Object.<anonymous> (HelloWorld.js:2:9)\n    at Module._compile (internal\/modules\/cjs\/loader.js:959:30)\n    at Object.Module._extensions..js (internal\/modules\/cjs\/loader.js:995:10)\n    at Module.load (internal\/modules\/cjs\/loader.js:815:32)\n    at Function.Module._load (internal\/modules\/cjs\/loader.js:727:14)\n    at Function.Module.runMain (internal\/modules\/cjs\/loader.js:1047:10)\n    at internal\/main\/run_main_module.js:17:11\n<\/pre>\n<p><strong>We got this error because <code>num<\/code> is not an string, it is number. <\/strong><\/p>\n<p>Convert num to string using <code>toString()<\/code> method and it will work fine.<\/p>\n<pre code=\"javascript\" mark=\"18\" title=\"Calling map() on array\">\nvar num=1.23;\nnum=num.toString().replace(\/\\.\/g,'');\nconsole.log(num);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre title=\"Output\">\n123\n<\/pre>\n<p>You can also check if it is type of string before calling replace() to make sure that <code>replace()<\/code> is being called on string only.<\/p>\n<pre code=\"javascript\" mark=\"18\" title=\"Calling map() on array\">\nvar num=1.23;\nnum=typeof num === 'string' ?num.replace(\/\\.\/g,''):'';\nconsole.log(num);\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre title=\"Output\">\n\n<\/pre>\n<p>As you can see  <code>num<\/code> is not a string, so result is empty string.<\/p>\n<p>We used ternary operator to check if <code>num<\/code> is string or not. If it is a string, call the replace method, otherwise return empty string.<\/p>\n<blockquote>\n<p>Read also: <a href=\"https:\/\/java2blog.com\/typeerror-map-is-not-function-javascript\/\" title=\"map is not a function in JavaScript\">map is not a function in JavaScript<\/a>.<\/p>\n<\/blockquote>\n<p>That&#8217;s all about how to resolve <code>TypeError: replace is not a function in JavaScript<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s see with help of simple example where we want to remove . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[42],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20056"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20056"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20056\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}