JavaScript throws "x is not a function" when you call something that is undefined, an object or another value. Learn the usual causes and how to track each one down.
The short answer
#You are calling something with parentheses, but the value at that moment is not a function. It is usually undefined (a typo, a missing export or a method on the wrong object), but it can be a string, a number or a plain object. Log the value just before the call to see what it really is, then fix where it comes from.
_10const user = { name: 'Ada' };_10_10user.getName(); // TypeError: user.getName is not a function_10_10console.log(typeof user.getName); // "undefined"
Why it happens
#In JavaScript, x() means "look up x, then call it". The lookup always succeeds for a property access: a missing property just gives undefined. The error only appears at the call step, when the engine finds that the value has no call behaviour. That is why the message names the expression you called, not the place where the wrong value came from.
The common causes are:
- A typo or wrong casing, such as
document.getElementByIDinstead ofdocument.getElementById. - Calling an array method on something that is not an array, such as
.mapon a plain object, or.forEachon anHTMLCollectionfromgetElementsByClassName. - A default export imported as a named export, or the other way round.
- A variable that shadows a function, for example a parameter called
formathiding an importedformatfunction. - Calling a method after losing
this, or calling a value returned from an API that is a promise or plain data, not a function. - A missing semicolon before a line that starts with
(or[, which makes JavaScript treat the previous line's value as a function.
How to fix it
#Check what the value actually is
Put a log or a breakpoint right before the failing call. typeof tells you the type, and logging the parent object shows which properties really exist.
_10console.log(typeof items.map, items);
If items turns out to be an object such as { data: [...] }, the fix is items.data.map(...), not a change to the call.
Match your imports to the exports
A mismatch between named and default exports gives you undefined or a module object instead of the function.
_10// utils.js_10export default function slugify(text) { /* ... */ }_10_10// app.js_10import { slugify } from './utils.js'; // undefined_10import slugify from './utils.js'; // correct
In CommonJS, module.exports = fn and module.exports = { fn } need different require code for the same reason.
Convert array-like values before calling array methods
querySelectorAll returns a NodeList, arguments is array-like, and a Map or Set has its own methods. Turn them into real arrays first.
_10const links = Array.from(document.querySelectorAll('a'));_10const hrefs = links.map((link) => link.href);
Guard optional callbacks
When a function is genuinely optional, use optional call syntax so a missing callback does nothing instead of throwing.
_10function save(data, onDone) {_10 // ..._10 onDone?.(data);_10}
Only do this when the function really is optional. If it should always exist, the error is telling you about a real bug.
Add the missing semicolon
Without a semicolon, these two lines run as one expression and try to call the string.
_10const message = 'hello'_10(() => console.log('run'))(); // TypeError: "hello" is not a function
End the first line with a semicolon, or let a formatter such as Prettier insert them for you.
Common pitfalls
#- Assuming the error is at the call site. The bad value was usually created earlier, so trace it back to where it was assigned.
- Calling
await fetch(url).json(). Theawaitapplies to the whole chain, so.jsonis looked up on the promise. Use(await fetch(url)).json()or two lines. - Reusing a name. Declaring
let filter = []in a scope where you also call afilterfunction hides the function. - Extracting a method and calling it alone, such as passing
user.saveas a callback when the method relies onthis. Bind it or wrap it in an arrow function. - Calling a function before a script that defines it has loaded, for example a CDN script with
deferthat has not run yet.
Related reading
#Was this resource helpful?