Why JavaScript error “is not a function” occurs

JavaScript error “is not a function” is pretty straight forward. Sometime you will get Uncaught TypeError. So the question is why this type of error occurs?

Suppose you declared a variable i.e x and call it as JavaScript Function i.e x(), then you will get this type of error. Let’s take an example-

function foo(){
	var bar = 5;
	bar();
}

In the above function, you declared a variable bar with a value 5, and the next line you called the variable as a function. So you will get Uncaught TypeError: bar is not a function.

You may also like to read: Check if a string contains a specific word in PHP?

Another example of JavaScript error “is not a function” is-

Suppose If you have created a function called setCounter(), and you call it like setCountar(). So there is no function exist in your code called setCountar(). Basically this is a spelling mistake. So you will get the error Uncaught TypeError: setCountar is not a function.

So this type of error generally occurs if you make a spelling mistake or if you try to call a variable as a function.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment