Fix JavaScript 'Undefined is not a function' Error

Updated 2026-08-13

Programmer code screen
Photo: Matthew (WMF) / Wikimedia Commons (CC BY-SA 3.0)

The Error

TypeError: undefined is not a function
TypeError: something.method is not a function
Cannot read property 'map' of undefined

What It Means

You tried to call a function that doesn’t exist or is undefined.

Quick Fix

Option 1: Check for Typos in Function Name

WRONG:

const names = ['Alice', 'Bob'];
names.map((name) => name.toUpperCase());  // Wrong: 'map' is correct

// But what if you typo it:
names.mapp((name) => name.toUpperCase());  // ERROR: undefined is not a function

RIGHT:

names.map((name) => name.toUpperCase());

Check:

Option 2: Variable is Undefined

WRONG:

let myFunction;  // Declared but not assigned
myFunction();  // ERROR: undefined is not a function

RIGHT:

let myFunction = () => {
  console.log('Hello');
};
myFunction();  // Works

Option 3: Function Doesn’t Exist on Object

WRONG:

const obj = { name: 'John' };
obj.greet();  // ERROR: obj.greet is undefined

RIGHT:

const obj = {
  name: 'John',
  greet() {
    return 'Hello ' + this.name;
  }
};
obj.greet();  // Works

Common Causes

CauseExampleFix
Typo in method namearray.mapp()Use array.map()
Variable is undefinedlet x; x()Define function before calling
Wrong object typenumber.map()Numbers don’t have .map() use arrays
Method doesn’t existmyObj.badMethod()Define method on object
Wrong scopethis.method outside objectUse correct context
Import/require issueModule not importedAdd import or require

Step-by-Step: Debug the Error

Scenario: Error says “undefined is not a function” on line 5

Steps:

  1. Check line 5 for function call:

    // Line 5:
    myFunction();  // Is myFunction defined?
  2. Check function is defined before line 5:

    const myFunction = () => {
      console.log('hi');
    };
    
    myFunction();  // Now it works
  3. If it’s a method, check object has it:

    const user = {
      getName() {
        return this.name;
      }
    };
    
    user.getName();  // Works
    user.getAge();   // ERROR: undefined is not a function (doesn't exist)
  4. Check for typos in method name:

    const arr = [1, 2, 3];
    arr.map(x => x * 2);     // Correct: 'map'
    arr.mapp(x => x * 2);    // ERROR: typo

Array Methods (Most Common)

These only work on arrays, not other types

// CORRECT - array methods
const arr = [1, 2, 3];
arr.map(x => x * 2);
arr.filter(x => x > 1);
arr.reduce((a, b) => a + b);
arr.forEach(x => console.log(x));

// WRONG - these aren't arrays
const num = 5;
num.map(x => x * 2);  // ERROR: undefined is not a function

const str = 'hello';
str.map(x => x);  // ERROR: undefined is not a function (strings don't have map)

// USE INSTEAD:
Array.from(str).map(x => x);  // Convert string to array first

Check if Function Exists (Defensive)

// Safe check
if (typeof myFunction === 'function') {
  myFunction();
} else {
  console.log('Function not defined');
}

Async/Await Issue

// WRONG - forgetting await
async function getData() {
  const result = fetchData();  // Returns Promise, not data
  result.map(x => x);  // ERROR: undefined is not a function
}

// RIGHT - use await
async function getData() {
  const result = await fetchData();  // Wait for Promise
  result.map(x => x);  // Now it's the actual data
}

Module Import Issues

// WRONG - forgot to import
function myFunction() { }
myFunction();  // ERROR: not defined

// RIGHT - define or import first
import { myFunction } from './helpers';
myFunction();  // Works

// Or define locally
const myFunction = () => { };
myFunction();  // Works

React Specific

// WRONG - method not bound
class MyComponent extends React.Component {
  handleClick() {
    console.log(this);  // 'this' is undefined
  }
  
  render() {
    return <button onClick={this.handleClick}>Click</button>;
    // ERROR when clicked: undefined is not a function
  }
}

// RIGHT - use arrow function or bind
class MyComponent extends React.Component {
  handleClick = () => {
    console.log(this);  // 'this' is correct
  }
  
  render() {
    return <button onClick={this.handleClick}>Click</button>;
  }
}

Checklist

Last updated: 2026-08-13