0%

javascript-xxx is not a function

Have you ever seen this error: “TypeError: xxx is not a function”? This error occurs when you try to call a function that does not exist.

1
2
3
4
5
6
const person = {
name: 'Philip',
age: 18,
};

person.getAge(); // TypeError: person.getAge is not a function

You can solve this by the optional chaining operator ?.:

1
person.getAge?.(); // nothing output

However, if there is a property in the object has the same name by accident, you will still get that error

1
2
3
4
5
6
7
const person = {
name: 'Philip',
age: 18,
getAge: 18,
};

person.getAge?.(); // TypeError: person.getAge is not a function