Implement instanceof

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement instanceof

Approach

Walk the prototype chain of the target object using Object.getPrototypeOf in a while loop. At each step, compare the current prototype to Constructor.prototype. Return true on a match or false when the chain ends at null.

Complexity Analysis

Time
O(d)
Space
O(1)

Pattern

Prototype Chain Traversal

Why It Works

The instanceof operator checks if Constructor.prototype exists anywhere in the object prototype chain. Walking the chain with getPrototypeOf replicates this lookup exactly.

Updated Mar 2026