In this article, I will help you to know the cause of the error “TypeError: split is not a function” in JavaScript and give some examples to fix it easily. Let’s go into detail now.
Why does the “TypeError: split is not a function” in JavaScript happen?
There are some cases where your program will throw the above error, but the most common one is using the split() method on a value that is not of type string.
The error message occurs as follows:
Uncaught TypeError: split is not a function
Example:
var date = new Date(); var str = (date).slice(1, 10); console.log(str);
Output:
Uncaught TypeError: date.split is not a function
Example:
var date = new Date(); var str = (date).slice(1, 10); console.log(str);
Output:
Uncaught TypeError: date.split is not a function
Above, I used two variables with a different value than the string to call the split() method, and the result is that the two program examples both throw the Uncaught TypeError: slice is not a function.
How to fix this error?
Fixing this error is not complicated. You must convert its value into a string and use the typical split() method.
Here are some solutions to fix this error.
Use toString() method
toString() method helps us convert a certain value into a string. Most cases of converting some values into a string using this method because it appears in most objects in JavaScript.
Example:
// Example with Number var num = 1431817912; // Convert Date to String var strNum = num.toString(); console.log(strNum.split("1")); // Example with Date var date = new Date(); // Convert Date to String var strDate = date.toString(); var strDate1 = date.toDateString(); console.log(strDate.split(" ")); console.log(strDate1.split(" "));
Output:
(5) ['', '43', '8', '79', '2']
(8) ['Wed', 'Nov', '09', '2022', '17:58:02', 'GMT+0700', '(Indochina', 'Time)']
(4) ['Wed', 'Nov', '09', '2022']
In the above example, we see that for the date example, we have two ways to use: the toString() method and the toDateString() method to convert to string. Still, the two returned results are different because the toDateString() method will only have the date and year without the time. So depending on your needs, you will choose between the toDateString() or toString() method.
Plus an empty string
For this method, you use the (+) operator to add the value of the Number or Date variable to convert it to a string.
Example:
// Example with Number var num = 1431817912; // Convert Date to String var strNum = num + ""; console.log(strNum.split("1")); // Example with Date var date = new Date(); // Convert Date to String var strDate = date + ""; console.log(strDate.split(" "));
Output:
(5) ['', '43', '8', '79', '2']
(8) ['Wed', 'Nov', '09', '2022', '17:58:02', 'GMT+0700', '(Indochina', 'Time)']
Summary
In this article, I have shown you how to fix the error “TypeError: split is not a function” in JavaScript. Try it again. It will help you understand and remember it longer. I hope this article helps you and your program. Good luck.
Maybe you are interested:
- TypeError: getFullYear() is not a Function in JavaScript
- TypeError: toLocaleDateString is not a function in JavaScript
- TypeError: Cannot read property ‘filter’ of Undefined in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css