TryBugHunt

TypeError: x is not a function

TypeError: x is not a function in JavaScript means you called something that is not a function — undefined, a string, an object. The message names exactly what you tried to call.

JavaScriptPattern: Type errors

How do I fix TypeError: x is not a function?

TypeError: x is not a function means the thing before ( ) is not a function when the call runs. Common causes are a misspelled method like getElementByID, an optional callback that was not passed, a variable that replaced the function, or a default import of a named export. Log typeof x before the call to see which.

  1. 1. Log typeof firstconsole.log(typeof x) right before the call. undefined means a typo, missing argument or bad import.
  2. 2. Check method spellingMethod names are case-sensitive. Compare with the docs, for example getElementById, not getElementByID.
  3. 3. Check imports and reassignmentsUse import { name } for named exports. Search for other assignments to the same name.

What does TypeError: x is not a function mean?

JavaScript checks callability at the moment of the call. "x is not a function" means the expression before the ( ) evaluated to something else. If it is undefined, the name is wrong or never set; if it is defined, a variable has taken the function's place.

Why does TypeError: x is not a function happen?

1. A misspelled method name

Method names are case-sensitive; a wrong one is undefined, and calling undefined throws.

Breaks

document.getElementByID("app");

Works

document.getElementById("app");

2. An optional callback that was not passed

Calling a missing callback throws only for the callers that omit it.

Breaks

function load(url, onDone) {
  fetch(url).then(r => onDone(r));
}
load("/api");

Works

function load(url, onDone = () => {}) {
  fetch(url).then(r => onDone(r));
}

3. A variable that shadows the function

Assigning to the same name later replaces the function.

Breaks

function total(items) { /* ... */ }
total = 0;
total(items);

Works

function total(items) { /* ... */ }
let sum = total(items);

4. Default versus named import

In a bundler (webpack, Next.js), importing a named export as default gives undefined, so the call throws. Native ES modules fail earlier: does not provide an export named 'default'.

Breaks

// format.js: export function format() {}
import format from "./format.js";
format(date);

Works

import { format } from "./format.js";
format(date);

How to find it in your own code

console.log(typeof thing) right before the call. undefined means a typo, a missing argument, or a bad import; anything else means something overwrote the function — search for assignments to that name.

Practise type errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all type errors →