Skip to content

TypeError: x is not a function

Error message

                x is not a function
              

Quick summary

You used parentheses to call a value, but that value is not a function — it's a number, string, object, or `undefined`.

Why this happens

  • Property or method name is misspelled (e.g., arr.legnth() instead of arr.length)

  • The value is undefined or null because a lookup or API call failed

  • A built-in method was overridden or the object is the wrong type

Minimal example

✗ Broken code
const num = 5;
num();
✓ Fixed code
const fn = () => 5;
fn();

num is a number, not a function — calling it with () throws a TypeError. Assign a function instead.

How to diagnose

  • Log the value and its type with console.log(typeof value) before the call

  • Check if the function exists on the object with typeof obj.method

  • Verify the object is the type you expect it to be

How to fix

  • Fix the method name to match the real method (e.g., length, not legnth)

  • Ensure the value is actually a function before calling it

  • Check that the object is the correct type before calling its methods

How to prevent

  • Use typeof checks before calling uncertain functions

  • Use TypeScript to catch type mismatches at compile time

Related Resources

Related Glossary

Related Lessons

Related Practice

← Back to language