Skip to main content

How to Resolve "TypeError: .split is not a function" Error in JavaScript

The TypeError: x.split is not a function is a common error that occurs when you try to call the String.prototype.split() method on a value that is not a string. This typically happens when you mistakenly assume a variable holds a string, but it actually holds a number, an object, null, or undefined.

This guide will explain the common scenarios that cause this error and show you the correct, modern solutions for both guarding against the error and correctly splitting your data.

The Core Problem: .split() is a String-Only Method

The .split() method is a function that exists exclusively on the String.prototype. It is designed to divide a string into an ordered list of substrings. It does not exist on any other data type. When you try to call it on a number, an array, a plain object, null, or undefined, JavaScript throws a TypeError.

Example of problem:

// Problem: `myData` is a number, not a string.
let myData = 12345;

// ⛔️ TypeError: myData.split is not a function
let result = myData.split('');
note

The key to solving this error is to ensure you are only calling .split() on a true string.

Cause 1 (Most Common): The Variable is a Number

This is a very frequent source of the error, especially when you want to split a number into its individual digits.

Example of problem:

// Problem: `pincode` is a number, not a string.
let pincode = 1234;

// ⛔️ TypeError: pincode.split is not a function
let digits = pincode.split('');

Solution: You must first convert the number to a string before you can call a string method on it. The String() constructor is the simplest way to do this.

let pincode = 1234;

// ✅ Correct: First, convert the number to a string.
let pincodeString = String(pincode);

let digits = pincodeString.split('');
console.log(digits); // # Output: ['1', '2', '3', '4']

Cause 2: The Variable is an Object or another Non-String Type

You might have an object and try to call .split() on it directly, when your intent was to split one of its properties.

Example of problem:

// Problem: `user` is an object.
let user = { fullName: 'Alice Smith', id: 1 };

// ⛔️ TypeError: user.split is not a function
user.split(' ');

Solution: you must access the specific string property on the object that you intend to split.

// ✅ Correct: Call `split` on the string property.
let user = { fullName: 'Alice Smith', id: 1 };
let nameParts = user.fullName.split(' ');

console.log(nameParts); // # Output: ['Alice', 'Smith']

Cause 3: The Variable is undefined or null

If a variable has not been initialized or a function did not return a value, it may be undefined.

Example of problem:

let myData; // `myData` is `undefined`

// ⛔️ TypeError: Cannot read properties of undefined (reading 'split')
myData.split(',');

Solution: guard your code to ensure you only call split on a valid string. Optional chaining (?.) is a modern and concise way to do this.

let myData; // `myData` is `undefined`

// ✅ Safe: If `myData` is null or undefined, the expression returns `undefined`.
let result = myData?.split(',');

console.log(result); // # Output: undefined (no error thrown)

You can combine this with the nullish coalescing operator (??) to provide a default value.

// If the split operation results in `undefined` (because the variable was nullish),
// fall back to an empty array.
let safeResult = myData?.split(',') ?? [];
console.log(safeResult); // # Output: []

Practical Example: A Robust Splitting Function

This function demonstrates how to safely split a value, handling different data types gracefully.

function safeSplit(data, separator) {
// Guard against null and undefined
if (data == null) {
return [];
}

// Convert numbers or other primitives to strings before splitting
let stringToSplit = String(data);

return stringToSplit.split(separator);
}

// Example Usage:
console.log(safeSplit('hello world', ' ')); // # Output: ['hello', 'world']
console.log(safeSplit(12345, '')); // # Output: ['1', '2', '3', '4', '5']
console.log(safeSplit(null, ',')); // # Output: []
console.log(safeSplit({a: 1}, '')); // # Output: ['[', 'o', 'b', 'j', 'e', 'c', 't', ' ', 'O', 'b', 'j', 'e', 'c', 't', ']']

Conclusion

The TypeError: .split is not a function is a clear signal that you are trying to use a string method on the wrong data type.

  • Remember that .split() is only available for strings.
  • If you have a number, you must convert it to a string first: String(myNum).split(...).
  • If you have an object, you must access one of its properties that is a string: myObj.myProp.split(...).
  • Use optional chaining (?.) to prevent errors when a variable might be null or undefined.