Write a function that checks if a given value is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.
There are no constraints on the data types that can be passed to the function. For example, the value or the class could be undefined.
Example 1:
Input: func = () => checkIfInstanceOf(new Date(), Date) Output: true Explanation: The object returned by the Date constructor is, by definition, an instance of Date.
Example 2:
Input: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
Output: true
Explanation:
class Animal {};
class Dog extends Animal {};
checkIfInstanceOf(new Dog(), Animal); // true
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.
Example 3:
Input: func = () => checkIfInstanceOf(Date, Date) Output: false Explanation: A date constructor cannot logically be an instance of itself.
Example 4:
Input: func = () => checkIfInstanceOf(5, Number) Output: true Explanation: 5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".
Problem Overview: You are given an object and a class (constructor). The task is to determine whether the object is an instance of that class. In other words, check whether the object's inheritance chain contains the provided class constructor.
Approach 1: Use Language-Specific Instance Checking (Time: O(1), Space: O(1))
Most languages provide a built‑in operator or function for checking class membership. In JavaScript you use instanceof, Python provides isinstance(), Java uses instanceof, and C# provides the is operator. These utilities internally inspect the object's type metadata or prototype chain and return a boolean result. Because the runtime handles the check natively, the operation is effectively constant time for typical use cases and requires no additional memory.
This approach is the most reliable and concise. It correctly handles inheritance, meaning subclasses also count as instances of the parent class. When writing production code or solving interview problems, this is almost always the preferred solution because it uses the language’s built‑in type system instead of re‑implementing it manually.
Approach 2: Manual Prototype Chain Walking (Time: O(h), Space: O(1))
In JavaScript, objects inherit from other objects through a prototype chain. Each object has an internal [[Prototype]] reference accessible through Object.getPrototypeOf() or __proto__. To determine if an object is an instance of a constructor, you can repeatedly move up this chain and compare each prototype with constructor.prototype.
The algorithm is straightforward: start with the object's prototype, iterate upward using Object.getPrototypeOf(), and stop if you either find the target prototype or reach null. If the target appears anywhere in the chain, the object is an instance of that class. The runtime cost depends on the depth of the inheritance chain (h), which is usually small but technically linear in the chain length.
This approach demonstrates a deeper understanding of how JavaScript implements object-oriented programming through prototypes rather than classical classes. It’s useful when implementing custom type utilities or when interviewers want to see knowledge of the prototype chain itself.
Recommended for interviews: Use the language’s built‑in instance check. Interviewers expect you to recognize the native operator (instanceof, isinstance, etc.) because it’s concise and idiomatic. The manual prototype traversal is still valuable to discuss because it shows you understand how instance checking works under the hood.
In this approach, we make use of direct language features to determine if an object is an instance of a given class. Most programming languages have built-in operators or functions to check instanceof relationships, such as 'instanceof' in JavaScript, or 'isinstance' in Python.
In JavaScript, the 'instanceof' operator checks if an object is an instance of a class. It walks up the prototype chain to see if the constructor appears anywhere in the object's prototype chain. This method is suitable for object instances and covers cases such as subclass inheritance.
JavaScript
Python
Java
C#
C
Time Complexity: O(n), where n is the depth of prototype chain.
Space Complexity: O(1), no extra space is used.
This approach is specifically for JavaScript where we manually traverse the prototype chain of the object to check if the class prototype is found in the chain.
This solution manually walks up the prototype chain by using 'Object.getPrototypeOf'. For each step in the chain, it verifies if the current prototype matches the prototype of the class, which ensures that subclass checks are also performed. This method provides finer control and understanding of the checking process.
JavaScript
C++
C
Time Complexity: O(n), where n is the depth of the prototype chain.
Space Complexity: O(1)
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Use Language-Specific Instance Checking | Time Complexity: O(n), where n is the depth of prototype chain. |
| Approach 2: Manual Prototype Chain Walking (JavaScript Specific) | Time Complexity: O(n), where n is the depth of the prototype chain. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Language-Specific Instance Checking | O(1) | O(1) | Default choice. Uses built-in operators like instanceof or isinstance for fast and reliable type checks. |
| Manual Prototype Chain Walking | O(h) | O(1) | When implementing custom instance logic or demonstrating understanding of JavaScript prototype inheritance. |
Check if Object Instance of Class - Leetcode 2618 - JavaScript 30-Day Challenge • NeetCodeIO • 4,189 views views
Watch 6 more video solutions →Practice Check if Object Instance of Class with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor