Languages
[Edit]
EN

JavaScript - check if object has property or function

14 points
Created by:
Selina-Miranda
737

In JavaScript, it is possible to check if the object has a property or method (function) in the following ways.

1. hasOwnProperty method example

Edit

This approach checks only entries that are directly inside the object.

2. in keyword example

Edit

This approach checks elements that are inside the prototype chain.

Note: be careful during using this approach because of inherited properties and methods e.g. true value for 'toString' in student expression.

3. Most common mistakes

Edit

3.1. in keyword for entry in prototype chain example

Edit

3.2. if instruction for undefined or null valued entries example

Edit

Alternative titles

  1. JavaScript - how to check if object property is defined?
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join