How to Fix 'TypeError: X is not a function' in JavaScript
The Error
TypeError: user.getName is not a function
TypeError: (intermediate value).someFunction is not a function
TypeError: "hello".toUppercase is not a function // note the typoThis error means JavaScript expected a function at a certain position, but found something else — usually undefined, null, or a value of the wrong type.
What Causes It
1. Variable is undefined or null
let greet;
greet(); // TypeError: greet is not a function
const options = null;
options.execute(); // TypeError: options is not a function
Fix: Always check that your function variable exists before calling it:
if (typeof greet === 'function') {
greet();
}2. Wrong Import or Missing Export
// module.js
export const sayHello = () => console.log('Hi');
// app.js
import { sayHelo } from './module.js';
sayHelo(); // TypeError: sayHelo is not a function (it's undefined)
Fix: Verify import names match their exports. Use an editor with import autocompletion.
3. Async Function Before Definition
// ❌ Called before declaration
execute(); // TypeError if variable is in TDZ
const execute = () => console.log('running');Fix: Use function declarations (which are hoisted) or call functions after their definition:
// Function declaration — hoisted, works
execute();
function execute() {
console.log('running');
}4. Object Property Typo
const person = {
getName() { return 'Alice'; }
};
person.getname(); // TypeError: person.getname is not a function (wrong case)
Fix: JavaScript is case-sensitive. getName and getname are different properties. Use autocomplete to avoid typos.
5. Method Extracted from Object Loses Context
const user = {
name: 'Alice',
greet() {
console.log(`Hello, ${this.name}`);
}
};
const greetFn = user.greet; // Function reference only
greetFn(); // TypeError: this.name is undefined — but not "not a function"
Note: This usually causes a different error (this is undefined), but can cause “not a function” if you extract a non-existent method.
How to Fix It
Step 1: Log the Value
Always check what you’re actually calling:
console.log('typeof myFunc:', typeof myFunc);
console.log('myFunc value:', myFunc);If typeof is 'undefined', 'string', 'number', 'object', or 'boolean' — it’s not a function.
Step 2: Check the Import
// Did you import correctly?
import { add } from './math.js'; // Is 'add' exported from math.js?
// Is the path correct?
import { add } from './math.js'; // Or is it './utils/math.js'?
Step 3: Verify Method Names
// Common mistakes
str.toUppercase() // Should be toUpperCase()
arr.forEech() // Should be forEach()
arr.puhs() // Should be push()
Step 4: Check for Property Shadowing
const data = {
value: 42,
process: function() { /* ... */ }
};
// If someone overwrites:
data.process = 123; // Now it's a number, not a function
data.process(); // TypeError!
Prevention
- Use TypeScript — catches “not a function” errors during compilation.
- Name consistently — use autocomplete and avoid manual typing for method names.
- Add null checks —
typeof fn === 'function'before calling. - Check exports — verify
exportandimportmatch exactly. - Use default function parameters safely:
function execute(callback = () => {}) {
callback(); // Safe — callback is always a function
}Summary
| Cause | Check | Fix |
|---|---|---|
| Undefined variable | typeof fn | Initialize the function |
| Wrong import | Import names | Fix import/export mismatch |
| Async timing | Hoisting | Use function declarations |
| Typo | Method spelling | Use autocomplete |
| Overwritten property | Object state | Avoid reassigning methods |
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro