Skip to content

JavaScript errorTypeError: “x is not a function”

The code stops with TypeError: something is not a function, such as data.map is not a function or this.setState is not a function. The code tried to call a value with (), and that value is not a function: it is undefined, an object, a string or a number. The name in the message tells you exactly which call failed.

Short answer

Look at the name in the message. For x.map is not a function, x is not an array: log it, and if it is an object from an API, use the array inside it (data.items.map) or Object.values(x). For a typo, check the exact spelling and case (getElementById, not getElementByID). For something you imported, check default versus named imports: import helpers from './helpers' and import { helpers } from './helpers' are different. If you forgot await, x is a Promise, which has no map: await it first. For this.x is not a function, this is a different object than you expect (a jQuery or event callback, a plain function inside a method): use an arrow function or bind it. If this is undefined, the error reads "Cannot read properties of undefined" instead.

Why the value is not a function, and the fix

  1. 01

    Calling an array method on something that is not an array

    “For example, JavaScript Objects have no map function, but the JavaScript Array object does.” MDN, TypeError: "x" is not a function

    map, filter, forEach and find exist on arrays only. API responses often wrap the list in an object ({ data: [...] }), return an object keyed by id, or send null when empty. Log the value and use the array inside it, or convert an object with Object.values(). A missing await gives a Promise instead of data (res.json().map fails), and array-likes such as querySelectorAll() results, Set, Map and arguments need Array.from(x) before map.

    const res = await fetch('/api/orders');
    const body = await res.json();
    const orders = Array.isArray(body) ? body : body.orders ?? [];
    orders.map((o) => o.id);
  2. 02

    A typo or wrong case in the name

    “Maybe there is a typo in the function name? Maybe the object you are calling the method on does not have this function?” MDN, TypeError: "x" is not a function

    JavaScript names are case sensitive, and a misspelled method is simply undefined. Compare the name with the documentation of the library or API, letter by letter.

  3. 03

    The import does not match the export

    “Ensure you are importing the module correctly.” MDN, TypeError: "x" is not a function

    If a module uses export default, import it without braces; if it uses a named export, import it with braces and the same name. With a bundler or CommonJS interop, mixing them gives you undefined or the whole module object, and calling it fails; native ES modules fail earlier with a SyntaxError such as “does not provide an export named”. Some CommonJS packages also need .default when imported into ES modules.

    // helpers.js
    export default function formatPrice() {}
    export function slugify() {}
    
    // app.js
    import formatPrice, { slugify } from './helpers.js';
  4. 04

    A callback lost its this, or a name is reused

    Passing a class method as a callback (onClick={this.handleClick}, setTimeout(obj.run)) detaches it from its object. Inside a class this is then undefined, so you usually see "Cannot read properties of undefined (reading 'setState')"; if the callback runs with another object as this (a jQuery or DOM event handler), you see "this.setState is not a function". Use an arrow function or bind it in the constructor. A property and a method with the same name also clash: the property wins, so the method looks missing.

    class Form extends React.Component {
      handleSubmit = () => {
        this.setState({ sent: true });
      };
    }

Checked against the vendor’s docs on : MDN: TypeError: "x" is not a function.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: “Cannot read properties of undefined” · “Cannot use import statement outside a module” · React “Invalid hook call”

Questions

More on this error

Why does data.map work sometimes and fail other times?

The value changes type: an empty state of undefined before loading, null when the API has no results, or an error object instead of a list when the request fails. Start with an empty array, check res.ok before reading the body, and only call map on an array.

What does “(intermediate value) is not a function” mean?

A line without a semicolon ran into the next line that starts with ( or [, so JavaScript tried to call the previous value. Add the missing semicolon at the end of the line before.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.