This article will demonstrate:
- How Does TypeError: callback is not a function in JavaScript Occur?
- How to fix TypeError: callback is not a function in JavaScript?
How Does “TypeError: callback is not a function in JavaScript” Occur?
The “TypeError: callback is not a function in JavaScript” occurs when the callback is provided to a function as an argument. Still, the function is called without passing the callback as a parameter.
Example
Here, we will define a function “calculation()” that takes “callback” as a parameter but doesn’t provide a callback when invoking the function:
return callback();
}
Call the “calculation()” function:
The output shows an error:
Let’s see how to fix the above-mentioned error.
How to Fix “TypeError: callback is not a function in JavaScript”?
To solve the specified error, define the callback function using the arrow function and then return it to the defined function:
return callback();
}
Call the function:
Or you can define the callback function inside the function call:
It can be observed that the mentioned error has been resolved successfully:
Here, we will perform an addition operation in a callback function by passing two parameters “a” and “b” and then call it by passing two arguments “5” and “8”:
var sum = a + b;
console.log("Sum is: " + sum);
}) {
return callback(5, 8);
}
Output
That’s how you fix the specified type error.
Conclusion
The “TypeError: callback is not a function in JavaScript” occurs when a function’s callback argument is specified, but the function is called without passing the callback as a parameter. For solving the specified error, define the callback function using the arrow function and then return it to the defined function. This article demonstrated the occurrence and solution for the given error.