---
title: "TypeError: x is not a function"
description: "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."
url: https://www.danny.engineering/answer/javascript/typeerror-x-is-not-a-function
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:58:00.000Z
tags: ["Javascript"]
---

# TypeError: x is not a function

## 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.


```javascript
const user = { name: 'Ada' };

user.getName(); // TypeError: user.getName is not a function

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.


```javascript
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.


```javascript
// utils.js
export default function slugify(text) { /* ... */ }

// app.js
import { slugify } from './utils.js'; // undefined
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.


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


```javascript
function save(data, onDone) {
  // ...
  onDone?.(data);
}
```


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.


```javascript
const message = 'hello'
(() => 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.

## Related reading

- [MDN: TypeError: "x" is not a function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function)
- [MDN: Optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
