How to fix typeerror: $ is not a function in JavaScript
In this article, you will learn about how to fix typeerror: $ is not a function in JavaScript.
“typeerror: $ is not a function” is a common error in JavaScript and it has occurred because of some silly mistakes of code such as when you are trying to call a value from a function but the value that you are calling is not a function. In this article, I will show you some reasons occurring this error and how to solve it. But let’s see the error first in the console:
The main reason for getting this error is because of making a typo error. See the solution for it:
// Wrong:
document.AddEventListener("click", function(){
// code...
});
//Correct:
document.addEventListener("click", function(){
// code...
});
You can see that it will be addEventListener() while I was giving AddEventListener(). Making this type of typo mistake may occur this problem.
Another reason for getting this error is if you are trying to call a map function inside an object. A JavaScript lets you call a map function inside an Array not in an Object. See the below code example:
//Wrong:
let age = {deven : 23, Alex: 21, sumona: 22};
age.map(function(age) {
return age + 2;
});
console.log(age)
//Output: Uncaught TypeError: age.map is not a function
//Correct:
let age = [23, 21, 22];
age.map(function(age) {
return age + 2;
});
console.log(age)
//Output : [ 23, 21, 22 ]
In general math, we may use brackets for multiplication for example, 5(2+2) = 20 in general math. But if you try to do the same in JavaScript you will get the error. Let’s see the below code example of both wrong and correct form of code for this:
//Wrong:
const Twenty = 5(2 + 2);
console.log(`5 x (2 + 2) is ${Twenty}` );
//Output: Uncaught TypeError: 5 is not a function
//Correct:**
const Twenty = 5 * (2 + 2);
console.log(`5 x (2 + 2) is ${Twenty}` );
//Output: 5 x (2 + 2) is 20
These are a few reasons for occurring “typeerror: $ is not a function” in JavaScript. If you ever get yourself stuck in this problem these are the way of how you can fix this problem.