Ad Space — Top Banner

is not a function

JavaScript Programming Language

Severity: Moderate

What Does This Error Mean?

This error means JavaScript found the variable you referenced, but it is not a function — so it cannot be called with (). You are trying to call something like a function, but it is actually a number, string, object, or undefined. This is a very common mistake, especially when working with objects and callbacks.

Affected Models

  • All browsers
  • Node.js
  • Deno
  • Any JavaScript runtime

Common Causes

  • Calling a variable that holds a number, string, or object as if it were a function
  • Misspelling a method name — JavaScript is case-sensitive
  • A property that used to be a function was overwritten with a non-function value
  • Trying to call a method that does not exist on that data type
  • A callback parameter was not passed properly and arrived as undefined

How to Fix It

  1. Read the full error message. It says something like 'myVar is not a function'. Log console.log(typeof myVar) to see what type it actually is.

    typeof will print 'number', 'string', 'object', 'undefined', or 'function'. If it is not 'function', that is your problem.

  2. Check for typos in the method or function name. Remember JavaScript is case-sensitive — toUpperCase() is different from touppercase().

    A single wrong letter in a method name will cause this error because JavaScript cannot find the correct method.

  3. If you are calling a method on an object, make sure that method exists for that type. For example, arrays have .map() but plain objects do not.

    Check the documentation for the data type you are working with to confirm the method name is correct.

  4. If you are passing a callback function as a parameter, make sure you are passing the function itself and not calling it. Write doSomething(myFunc) not doSomething(myFunc()).

    Writing myFunc() with parentheses calls the function immediately and passes its return value — not the function itself.

  5. Check if a variable was supposed to hold a function but got overwritten somewhere in your code. Search for where that variable name appears and what values it gets assigned.

    It is easy to accidentally do something like: let onClick = 'click'; and then later try to call onClick().

When to Call a Professional

This is a very common JavaScript error that all developers encounter. It does not require outside help — just careful checking of the variable's actual value. Once you find what the variable actually contains, the fix is usually obvious.

Frequently Asked Questions

Why does JavaScript say something is not a function when I can see it is a function in my code?

If you can see the function definition but still get this error, the variable probably got a different value assigned to it somewhere between definition and use. Add console.log(typeof myVar) just before the call to check. Also check if the variable name is used in more than one place — a later assignment could overwrite the function.

What does it mean when the error says 'undefined is not a function'?

It means the variable you tried to call is undefined — it was never assigned a value, or it was not passed as a parameter. This often happens with callback functions: if a callback is optional and was not provided, it will be undefined. Always check: if (typeof callback === 'function') { callback(); } before calling a callback.

Is there a quick way to check if something is a function before calling it?

Yes — use typeof: if (typeof myVar === 'function') { myVar(); } This checks the type before calling it, so you avoid the error. You can also use this pattern to provide a fallback if a callback was not passed.