x is not a function
An attempt was made to call a value as a function, but the value is not callable.
Usually happens because:
- ☑ Variable is a primitive value
- ☑ Typo in method name spelling
- ☑ Callback was not passed
🔍 Quick Checklist:
📊 Where this usually happens (estimated occurrence)
What is x is not a function?
The 'x is not a function' TypeError occurs when you attempt to invoke a variable or property as if it were a function (using parenthesis ()), but the variable contains a different type (such as a string, number, object, or undefined) that is not callable.
Common Causes
- Typo in method names or spelling errors.
- Reassigning a function variable to a primitive value.
- Attempting to call array methods on objects that are not arrays.
- Invoking callback functions that were not passed to the wrapper function.
Common Mistakes
- Assuming a third-party package exports a default function when it actually exports named bindings.
- Typoing capitalization on standard utility methods (e.g. calling getelementbyid instead of getElementById).
How to Fix
Framework-Specific Examples
Calling array functions (like map or filter) on elements that are strings or normal objects.
const data = { names: "John" };
try {
// Throws TypeError because names is a string, not an array
data.names.map(n => console.log(n));
} catch (err) {
console.error(err.message); // data.names.map is not a function
}Server Configuration Examples
Checking function parameters.
# Handled at code levelBest Practices
- Use TypeScript to guarantee that callables match signature definitions.
- Implement callback fallback assertions: `const fn = callback || (() => {})`.
Frequently Asked Questions (FAQ)
Q: What does 'x is not a function' mean?
It means you attempted to call a variable like a function using `()`, but the variable does not contain a callable function.
Q: How do I check if a variable is a function?
Verify using the typeof operator: `if (typeof variable === 'function')`.
Q: Can optional chaining be used on functions?
Yes. You can use the syntax `callback?.()` to invoke the function only if it is defined and callable.
Q: Why does this happen on array map?
Because the variable you are calling `.map()` on is not an array. It could be an object, null, or undefined. Print the variable type using `Array.isArray(variable)`.