Blog |

How to Handle JavaScript Uncaught TypeError: “x” is Not a Function

How to Handle JavaScript Uncaught TypeError: “x” is Not a Function
Table of Contents

The Javascript error TypeError: "x" is not a function occurs when there is an attempt to call a function on a value or object, which is not actually a function.

Error message:

TypeError: "x" is not a function

Error Type:

TypeError

 

What Causes TypeError: "x" is not a function

A TypeError: "x" is not a function in Javascript generally occurs in one of the following scenarios:

  • A typographical error in a function call.
  • Missing script library.
  • When a function is called on a property that is not actually a function.
  • A TypeError: "x" is not a function occurs when a function is called on an object that does not contain the called function.
  • When calling a built-in function that expects a callback function argument, which does not exist.
  • When the called function is within a scope that is not accessible

 

TypeError: "x" is not a function Examples

1. Typo

A typical scenario for the TypeError: "x" is not a function to occur is when there is a typo in the called function name:

var elem = document.getElementByID('ID');

Running the above code leads to the following Javascript error:

TypeError: document.getElementByID is not a function

The correct function name is getElementById():

var elem = document.getElementById('ID');

2. Object Does Not Contain Function

Another common cause for the TypeError: "x" is not a function is when a function is called an object that does not actually contain the function:

var foo = {
   bar: function() {
      console.log("bar called");
   }
};
foo.baz();

In the above example, the foo object contains a function bar(). However, the above code attempts to call the function baz(), which foo does not contain. Running the above code leads to the following Uncaught TypeError: "x" is not a function:

Uncaught TypeError: foo.baz is not a function

If the Javascript code is modified to call the correct function bar():

var foo = {
   bar: function() {
      console.log("bar called");
   }
};    
foo.bar();

The correct output is produced:

bar called

 

How to Fix Javascript TypeError: "x" is not a function

The TypeError: "x" is not a function can be fixed using the following suggestions:

  • Paying attention to detail in code and minimizing typos.
  • Importing the correct and relevant script libraries used in code.
  • Making sure the called property of an object is actually a function.
  • Making sure objects contain the called function to avoid TypeError: "x" is not a function.
  • Making sure functions passed in as callbacks do exist.
  • Making sure called functions are within the correct and accessible scope.

 

Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Javascript errors easier than ever. Sign Up Today!

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape