Javascript split is not a function – How to Fix split is not a function Error in JavaScript?

Javascript split is not a function: When we call the split() method on a value that is not of type string, we get the “split is not a function” error.

To resolve the error, convert the value to a string using the toString() function before calling the split method, or only call the split method on strings.

Let us see the examples to know how this error occurs and try to fix them.

index.js:

// Create a date object and store it in a variable
const gvn_str = new Date();

// Apply split() function on the date object and store it in a variable.
// Here it throws an error since split() function cannot be applied on an object
// It must be used only on strings
const result = gvn_str.split(' ');

Output:

const result = gvn_str.split(' ');
^

TypeError: gvn_str.split is not a function
at Object.<anonymous> (/tmp/ER7rLDlJor.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Explanation:

Here we called the String.split() method on an object and returned an error.

To fix the problem, only use the split() method on strings. Most values can be 
converted to strings using the function toString()  method.

Using toString() Method for Conversion

// Create a Date object using the new keyword and 
// store it in a variable
const gvn_string = new Date();
// Print the type of above variable using the typeof property
console.log(typeof gvn_string);

// Convert the above Date object to string using the 
// toString() method and split it based on '' using the 
// split() function
const rslt = gvn_string.toString().split(' ');
// Print the above result
console.log(rslt); 

Output:

object
[ 'Sat', 'Jun', '11', '2022', '18:50:00', 'GMT+0000', '(GMT)' ]

Explanation:

We were able to call the split() function on the date object since the 
toString() method converted this date object to a string.

Using Ternary Operator to Check if it is a String and do Conversion if Necessary

Split is not a function: Alternatively, before executing the split() method, you can check to see if the value is a string.

Example1: index.js

// Give some random value as static input and store it in 
// a variable
const gvn_str = 50;
// Check if the type of above variable value is a string using
// the ternary operator. 
// If it is a string then split it based on ' ' using the 
// split() function else print 'Not a String'
const rslt = typeof gvn_str === 'string' ? gvn_str.split(' '): 'Not a String';
// Print the above result
console.log(rslt); 

Output:

Not a String

Example2: index.js

// Give some random value as static input and store it in 
// a variable
const gvn_str = "Hello this is Btechgeeks";
// Check if the type of above variable value is a string using
// the ternary operator. 
// If it is a string then split it based on spaces(' ') using
// the split() function else print 'Not a String'
const rslt = typeof gvn_str === 'string' ? gvn_str.split(' '): 'Not a String';
// Print the above result
console.log(rslt); 

Output:

[ 'Hello', 'this', 'is', 'Btechgeeks' ]

Explanation:

  • To determine whether the gvn_str variable contains a string, we utilized a ternary operator.
  • If it satisfies the condition, it returns the value to the left of the colon(:) otherwise, it returns the value to the right.
  • If it is a string then split it based on spaces(‘ ‘) using the split() function else print ‘Not a String’.
  • If the value is an object, there’s a significant possibility you’re forgetting to access a specific property on which the split() method must be called.