TypeError: split is not a function in JavaScript

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

Let’s see with help of simple example.

Output

We got this error because countryList is not an string, it is an array.

Convert countryList to string using toString() method and it should work as expected.

Output:

toString() method converted array to string and we were able to call split() method on it.

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

Output:

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

We used ternary operator to check if countryList is string or not. If it is a string, call the split() method, otherwise return empty string.

If you are getting this issue, while working on document.location object then you need to first convert document.location to string using toString() method before calling split() method.

When you execute the code, you will get TypeError: .split is not a function as we are calling split() on document.location rather than string object.

If you print typeof for document.location, it will print object.

To solve the issue, convert document.location to string first.

Here is one of the solution:

Notice + '' at the end, it will convert document.location to string and we can call split() method on it.

Read also: map is not a function in JavaScript.

That’s all about how to resolve TypeError: split 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-62b889615dc3e652488088/] 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-62b889615e5d1936418357/] Output: […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.