Check if Object Instance of Class - Problem

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.

Input & Output

Example 1 — Basic Object Instance
$ Input: obj = {}, classFunction = Object
Output: true
💡 Note: Empty object {} has Object.prototype in its prototype chain, so it's an instance of Object
Example 2 — Null Class Function
$ Input: obj = {}, classFunction = null
Output: false
💡 Note: When classFunction is null, we can't check instanceof, so return false
Example 3 — Number Instance
$ Input: obj = 5, classFunction = Number
Output: true
💡 Note: Number 5 is an instance of the Number constructor function

Constraints

  • No constraints on data types
  • obj or classFunction can be undefined/null
  • Must handle all JavaScript types

Visualization

Tap to expand
instanceof Check: Prototype Chain Traversalobjobj.__proto__(prototype)Object.prototypenullClassFunction.prototypeMATCH!Result:trueTraverse prototype chain until match found or null reached
Understanding the Visualization
1
Input
Object and class function to check
2
Prototype Chain
Walk up the object's prototype chain
3
Match Check
Compare with class function's prototype
Key Takeaway
🎯 Key Insight: The instanceof operator works by walking up the prototype chain until it finds the constructor's prototype or reaches null
Asked in
Google 35 Facebook 28 Amazon 22
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen