More JavaScript answers

by Danny
Published 3 min read

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.


_10
const user = { name: 'Ada' };
_10
_10
user.getName(); // TypeError: user.getName is not a function
_10
_10
console.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.getElementByID instead of document.getElementById.
  • Calling an array method on something that is not an array, such as .map on a plain object, or .forEach on an HTMLCollection from getElementsByClassName.
  • A default export imported as a named export, or the other way round.
  • A variable that shadows a function, for example a parameter called format hiding an imported format function.
  • 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.


_10
console.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
_10
export default function slugify(text) { /* ... */ }
_10
_10
// app.js
_10
import { slugify } from './utils.js'; // undefined
_10
import 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.


_10
const links = Array.from(document.querySelectorAll('a'));
_10
const 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.


_10
function 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.


_10
const 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(). The await applies to the whole chain, so .json is 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 a filter function hides the function.
  • Extracting a method and calling it alone, such as passing user.save as a callback when the method relies on this. 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 defer that has not run yet.
#

Was this resource helpful?