[Solved] TypeError: split is not a function

Total
0
Shares

If we call the split() method on the value that is not of a string type, JavaScript will throw a TypeError: split is not a function.

In this tutorial, we will look at what is TypeErrror: split is not a function error and how to resolve them with examples.

What is TypeError: split is not a function error?

Let us take a simple example to demonstrate this issue

// documnet.location returns an object
const str = document.location
path = str.split("/")

Output

TypeError: str.split is not a function

In the above example, we have declared a variable and assigned the object into it.

In the next statement, we call the String.split() method on the location object, and hence we get a TypeError: split is not a function.

We can also check the variable type using typeof() to confirm the datatype.

const str = document.location
console.log("The type of variable is",typeof(str))

Output

The type of variable is object

How to fix TypeError: split is not a function error?

The String.split() method can only be used on the string values and not on any other types. 

There are two ways to fix this issue in JavaScript.

Solution 1: Convert the value into a string

We can easily resolve the issue by converting the location object into a string before calling the split() method.

If we know the value can be converted to a valid string, then we can use the toString() method in JavaScript that returns the string representing the object. 

Let us take an example to resolve the issue using the toString() method.

// Get the location and convert to String object
const str = document.location.toString()
console.log(str)
console.log("The object type is", typeof(str))

// Split the string object into an Array
path = str.split("/")
console.log(path)

Output

https://itsjavascript.com/javascript-typeerror-string-split-is-not-a-function

The object type is string

['https:', '', 'itsjavascript.com', 'javascript-typeerror-string-split-is-not-a-function']

Solution 2 – Performing the type check

We can also perform a type check on the variable to check if it’s a string before calling the split() method.

Example – Type check using if/else

// Get the location and convert to String object
const str = document.location.toString()

//Split the string object into an Array
if (typeof str === 'string') {
    path = str.split("/")
    console.log(path)
}
else {
    console.log("Not a vaild string object")
}

Output

['https:', '', 'itsjavascript.com', 'javascript-typeerror-string-split-is-not-a-function']

Example – Type check using ternary operator

const str = document.location.toString()
const result = typeof str === 'string' ? str.split('/') : "";
console.log(result)

Output

['https:', '', 'itsjavascript.com', 'javascript-typeerror-string-split-is-not-a-function']

Conclusion

The TypeError: split is not a function occurs if we call a split() method on the value that is not of a type string. We can resolve the issue by converting the value into string before calling the split() method or by performing a type check; we can mitigate this error.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript Anonymous Functions

JavaScript Anonymous Functions

Table of Contents Hide Anonymous Functions Syntax Example 1: Anonymous Functions in JavaScriptExample 2: Anonymous Functions with arguments in JavaScriptExample 3: Using anonymous functions as arguments of other functionsExample 4: Immediate…
View Post