JavaScript error: “is not a function”

For more generic advice on debugging this kind of problem MDN have a good article TypeError: “x” is not a function:

It was attempted to call a value like a function, but the value is not
actually a function. Some code expects you to provide a function, but
that didn’t happen.

Maybe there is a typo in the function name? Maybe the object you are
calling the method on does not have this function? For example,
JavaScript objects have no map function, but JavaScript Array object
do.

Basically the object (all functions in js are also objects) does not exist where you think it does. This could be for numerous reasons including(not an extensive list):

  • Missing script library
  • Typo
  • The function is within a scope that you currently do not have access to, e.g.:
var x = function(){
   var y = function() {
      alert('fired y');
   }
};
    
//the global scope can't access y because it is closed over in x and not exposed
//y is not a function err triggered
x.y();
  • Your object/function does not have the function your calling:
var x = function(){
   var y = function() {
      alert('fired y');
   }
};
    
//z is not a function error (as above) triggered
x.z();

Leave a Comment