Languages
[Edit]
EN

TypeScript - check if object has property or function

0 points
Created by:
Mariam-Barron
811

In TypeScript it is possible to check if object has property or method (function) in following ways.

1. hasOwnProperty method example

Edit

This approach checks only entries that are directly inside object.

Output:

2. in keyword example

Edit

This approach checks elements that are inside prototype chain.

Output:

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

Alternative titles

  1. TypeScript - 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