Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

TypeError: X is not a function - How to Fix (2026)

What is "is not a function" Error?

The error TypeError: X is not a function occurs when you try to call something as a function that isn't actually a function. This happens when the variable is undefined, null, or a different data type.

Common Causes

  • Variable is not a function (string, number, object, etc.)
  • Function name typo or doesn't exist
  • Calling a property that's not a function
  • Function not defined yet (hoisting issue)
  • Overwriting a function with another value

Quick Fix (TL;DR)

Quick Solution
// ❌ Problem
let myFunc = 'not a function';
myFunc(); // TypeError: myFunc is not a function

// ✅ Solution 1: Check if it's a function
if (typeof myFunc === 'function') {
    myFunc();
}

// ✅ Solution 2: Define the function properly
function myFunc() {
    console.log('I am a function');
}
myFunc(); // Works!

Common Scenarios & Solutions

Scenario 1: Variable is Not a Function

Trying to call a variable that contains a non-function value.

Problem
// Variable is a string
let calculate = 'sum';
calculate(5, 10); // TypeError: calculate is not a function

// Variable is a number
let process = 42;
process(); // TypeError: process is not a function

// Variable is undefined
let handler;
handler(); // TypeError: handler is not a function
Solution
// Check type before calling
let calculate = 'sum';
if (typeof calculate === 'function') {
    calculate(5, 10);
} else {
    console.error('calculate is not a function');
}

// Define as a function
let calculate = function(a, b) {
    return a + b;
};
calculate(5, 10); // 15

// Or use arrow function
let calculate = (a, b) => a + b;
calculate(5, 10); // 15

Scenario 2: Typo in Function Name

Calling a function with a typo in its name.

Problem
function calculateSum(a, b) {
    return a + b;
}

// Typo: calculateSun instead of calculateSum
calculateSun(5, 10); // TypeError: calculateSun is not a function

// Case sensitivity
CalculateSum(5, 10); // TypeError: CalculateSum is not a function
Solution
// Fix the typo
function calculateSum(a, b) {
    return a + b;
}

calculateSum(5, 10); // 15 - Works!

// Use IDE autocomplete to avoid typos
// Use ESLint to catch undefined functions

Scenario 3: Calling Object Property as Function

Trying to call an object property that's not a function.

Problem
const user = {
    name: 'John',
    age: 30
};

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

// Or accessing wrong property
const calculator = {
    sum: function(a, b) { return a + b; }
};

calculator.add(5, 10); // TypeError: calculator.add is not a function
Solution
// Add the method to the object
const user = {
    name: 'John',
    age: 30,
    greet: function() {
        return `Hello, I'm ${this.name}`;
    }
};

user.greet(); // "Hello, I'm John"

// Or use correct property name
const calculator = {
    sum: function(a, b) { return a + b; }
};

calculator.sum(5, 10); // 15

// Check if method exists
if (typeof user.greet === 'function') {
    user.greet();
}

Scenario 4: Array Method on Non-Array

Calling array methods on variables that aren't arrays.

Problem
let users = null;
users.map(u => u.name); // TypeError: users.map is not a function

// Or undefined
let items;
items.filter(i => i.active); // TypeError: items.filter is not a function

// Or wrong type
let data = 'not an array';
data.forEach(item => console.log(item)); // TypeError!
Solution
// Initialize as array
let users = [];
users.map(u => u.name); // Works (returns [])

// Check if array before using
let users = null;
if (Array.isArray(users)) {
    users.map(u => u.name);
}

// Use optional chaining with default
let users = null;
const names = users?.map(u => u.name) || [];

// Or nullish coalescing
let users = null;
(users ?? []).map(u => u.name);

Scenario 5: Function Overwritten

Accidentally overwriting a function with another value.

Problem
function calculate(a, b) {
    return a + b;
}

// Accidentally overwrite
calculate = 42;

calculate(5, 10); // TypeError: calculate is not a function

// Or in loop
for (let calculate = 0; calculate < 10; calculate++) {
    // calculate is now a number
}
calculate(5, 10); // TypeError!
Solution
// Use const to prevent reassignment
const calculate = function(a, b) {
    return a + b;
};

// calculate = 42; // TypeError: Assignment to constant variable

// Use different variable names
function calculate(a, b) {
    return a + b;
}

for (let i = 0; i < 10; i++) {
    // Use 'i' instead of 'calculate'
}

calculate(5, 10); // Works!

Best Practices

  • Check type before calling - Use typeof to verify it's a function
  • Use const for functions - Prevents accidental reassignment
  • Enable ESLint - Catches undefined functions during development
  • Use TypeScript - Type checking prevents these errors
  • Check array before methods - Use Array.isArray() or optional chaining
  • Avoid name collisions - Use unique, descriptive names
  • Initialize properly - Set correct initial values

Related Errors

Key Takeaways
  • This error occurs when calling something that is not a function
  • Common causes: typos, wrong variable type, undefined variables
  • Always check typeof before calling a function
  • Use const to prevent accidental function reassignment
  • Array methods only work on arrays - check with Array.isArray()
  • ESLint and TypeScript help catch these errors early

Frequently Asked Questions


Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.