“TypeError: toISOString is not a function” in JavaScript is a common error related to a string converter method. The below explanations can help you know more about the cause of this error and solutions.
How does the “TypeError: toISOString is not a function” in JavaScript happen?
Basically toISOString() is a method that belongs to new Date objects. Its functionality is to return a string in a simplified extended ISO formatted string. The “TypeError: toISOString is not a function” indicates that the method toISOString is being called on an object that is not created by the constructor new Date(), it can be other types such as string, boolean or even a Date object except the null type and undefined type.
For example, calling the method toISOString on an array will raise this error:
let array = [9,9,9,9];
console.log(array.toISOString());
Output
Uncaught TypeError: array.toISOString is not a function
at <anonymous>:2:19
Here is an another example of using this method on a Date type which is created by the Date() constructor instead of new Date():
let date = Date();
console.log(date.toISOString());
Output
Uncaught TypeError: date.toISOString is not a function
at <anonymous>:2:18
If you are not working with date objects but instead a defined type such as MyDate, maybe you get this error because you haven’t define the method before using
// Define a class MyDate
class MyDate{
MyDate(){}
// Declare anything but not the method toISOString
}
// Create a date object
let date = new MyDate();
// Calling toISOString method on MyDate objects
console.log(date.toISOString());
Output
VM584:10 Uncaught TypeError: date.toISOString is not a function
at <anonymous>:10:18
How to solve the error?
Use new Date() constructor
You must remember that, if you use the Date() constructor to create a date object, what you receive is just a string instead of any different type. In fact, using this constructor actually calls another constructor and its method, which is new Date().toString(). So if you want to get a date object, always use new Date() instead.
For example:
// Create a date object
let date = new Date();
// Calling toISOString method
console.log(date.toISOString());
Output
2022-10-30T14:40:25.603Z
Declare the method if not declared
You must define the function (the method) before you use it in an instance of your defined-class. For instance, you need to define toISOString() method in the class MyDate:
// Define new class MyDate
class MyDate{
MyDate(){}
// Declare the method toISOString
toISOString(){
return "LearnShareIT"
}
}
// Create a date object
let date = new MyDate();
// Calling toISOString method on MyDate objects
console.log(date.toISOString());
Output
LearnShareIT
If you don’t know your class has defined the method yet or not. You should check it by the following code:
// Checking if toISOString method in MyDate objects
console.log(date.toISOString);
Output
ƒ toISOString(){
return "LearnShareIT"
}
The example above shows that we actually have declared the function and hence the definition of it is returned in the console shown. Otherwise, if the object doesn’t have that method, then the command will return undefined, which means that you cannot call the method with that object.
Summary
We have learned how to deal with the error “TypeError: toISOString is not a function” in JavaScript. By checking the type of Date or new Date() and defining that method if it does not exist, you can tackle the problem. If you are working with Dates, you can consider looking at this article: How To Set Current Date On Input type= “date” Using JavaScript
Maybe you are interested:
- TypeError: createPopper is not a function in Bootstrap
- TypeError: getContext is not a function in JavaScript
- TypeError: Cannot read property ‘filter’ of Undefined in JavaScript

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R