Uncaught TypeError: 'undefined' is not a function
Reverse the order
For instance, while this code works as expected:
If you reverse the order of the statements the Uncaught TypeError: undefined is not a function error will occur:
So, It's better you double check that your scripts are being loaded correctly.
This weird behaviour depends on how you define the functions and when you call them.
This is because of something called hoisting!
- expression.that('returns').aFunctionObject(); // js
- execute -> expression.that('returns').aFunctionObject // what the JS engine does
That expression may be complex. So when you get undefined is not a function it means that expression did not return a function object. So you have to figure out what you are trying to execute isn't a function.
File order (library)
Sometimes the error happened because the library wasn't loaded (correctly). In case of jQuery , first you need to load in the jQuery library to be able to perform jQuery functions. It should be something like:
- any third party plugins you loading
- your custom JS
This is an error that occurs in Chrome when you call an undefined function. You can test this in the Chrome Developer Console and Mozilla Firefox Developer Console . Consider the following example:
When you execute the above code and then click on the page, it output the following error Uncaught TypeError: this.myMessage is not a function . It is because the anonymous function being executed is in the context of the document , whereas myMessage is defined on the window.
You can fix this issue by using the bind() method to pass the proper reference:
Uncaught means the error was not caught in a try-catch block . The TypeError object represents an error when a value is not of the expected type. Also, Undefined is the property of the global object. If you don’t assign any value to a variable is of type undefined .