object-oriented programming
method checking
property checking
programming tutorial
software development

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.

Browse interview questions

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.

javascript
1const user = {
2  name: "Ada",
3  save() {
4    console.log("saved");
5  }
6};
7
8console.log("name" in user);   // true
9console.log("save" in user);   // true

If you specifically need a method, also check that the property is a function:

javascript
if (typeof user.save === "function") {
  user.save();
}

If you want only own properties, use Object.hasOwn:

javascript
console.log(Object.hasOwn(user, "name"));

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.

python
1class User:
2    def __init__(self):
3        self.name = "Ada"
4
5    def save(self):
6        print("saved")
7
8user = User()
9
10print(hasattr(user, "name"))
11print(hasattr(user, "save"))

If you need to ensure the attribute is a method-like callable, combine getattr with callable:

python
save_attr = getattr(user, "save", None)
if callable(save_attr):
    save_attr()

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.

csharp
1using System;
2using System.Reflection;
3
4class User
5{
6    public string Name { get; set; } = "Ada";
7    public void Save() => Console.WriteLine("saved");
8}
9
10var type = typeof(User);
11Console.WriteLine(type.GetProperty("Name") != null);
12Console.WriteLine(type.GetMethod("Save") != null);

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 hasattr is 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, and typeof ... === "function" as needed.
  • In Python, use hasattr, getattr, and callable together 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.