How to check whether an object has certain method/property?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Checking whether an object has a given property or method depends on the language and on what exactly you mean by “has.” In some languages you want to know whether the name exists at all, while in others you need to know whether it is callable, inherited, public, or directly declared on the object itself.
The safest pattern is usually to test the specific capability you need rather than relying on assumptions about a concrete type. That is especially true in dynamic languages.
JavaScript: Property Existence Versus Callable Methods
In JavaScript, a property may exist directly on the object or be inherited from its prototype chain. The in operator checks both.
If you specifically need a method, also check that the property is a function:
If you want only own properties, use Object.hasOwn:
That avoids confusion with inherited members such as toString.
Python: hasattr And callable
Python uses attribute lookup rather than a strict property-versus-method split. hasattr tells you whether the object resolves a given attribute name.
If you need to ensure the attribute is a method-like callable, combine getattr with callable:
This is often better than checking only hasattr, because an attribute can exist without being something you can invoke.
C# And Reflection
In statically typed languages such as C#, the question often appears in generic code, serializers, or plugin systems. Reflection lets you inspect members by name.
Reflection is powerful, but it is slower and more verbose than normal static access, so it is best used when the member name is truly dynamic.
Prefer Capability Checks Over Type Assumptions
In dynamic code, the deeper question is often “can I call this behavior safely?” rather than “is this object an instance of class X?” That is the essence of duck typing.
For example, in JavaScript or Python, it is usually more robust to check whether the object exposes the operation you need than to tie the code to a specific constructor or inheritance tree.
Beware Of Side Effects In Dynamic Lookups
Some languages and frameworks implement attributes through descriptors, proxies, or magic methods. That means “checking whether a property exists” can trigger work or behave differently from a plain dictionary lookup.
In Python, for example, hasattr can swallow exceptions raised during attribute resolution. In JavaScript, proxies can intercept property existence checks. That is one reason to prefer the most direct and explicit test your language offers.
Common Pitfalls
- Checking only for existence when you really need to know whether a member is callable.
- Confusing own properties with inherited properties in JavaScript.
- Overusing reflection in statically typed languages when normal access would be simpler.
- Assuming
hasattris always side-effect free in Python. - Treating member-name checks as a substitute for better interface design.
Summary
- The right check depends on the language and on whether you care about existence, callability, inheritance, or visibility.
- In JavaScript, use
in,Object.hasOwn, andtypeof ... === "function"as needed. - In Python, use
hasattr,getattr, andcallabletogether for method checks. - In C#, use reflection when the member name is dynamic.
- Prefer testing for the capability you need rather than making unnecessary type assumptions.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.