TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
instanceof operator is a binary operator that tests whether an object has the prototype property of a constructor function or class in its prototype chain, or if the right-hand operand implements a custom type-checking routine via Symbol.hasInstance. It evaluates the relationship by reference and returns a boolean value.
object: The value (typically an object instance) to be evaluated.constructor: The object, constructor function, or class against which the evaluation is performed.
Internal Mechanics
When the expressionA instanceof B is evaluated, the JavaScript engine executes the internal InstanceofOperator(A, B) routine, which proceeds as follows:
- Target Type Check: The engine verifies that
Bis an object. IfBis a primitive value, it throws aTypeError. Symbol.hasInstanceDelegation: The engine checks ifBexposes a method keyed by the well-known symbolSymbol.hasInstance.- If present, the engine calls this method with
Aas the argument, coerces the result to a boolean, and returns it. - Note: All standard functions and classes inherit the built-in
Function.prototype[Symbol.hasInstance]method. This built-in method is what explicitly invokes theOrdinaryHasInstanceroutine (detailed below) to perform the standard prototype chain traversal.
- If present, the engine calls this method with
- Callable Check: If
Symbol.hasInstanceis absent, the engine verifies thatBis callable (possesses a[[Call]]internal method). IfBis not callable, it throws aTypeError. OrdinaryHasInstanceInvocation: IfBis callable but lacksSymbol.hasInstance, the engine directly invokes theOrdinaryHasInstance(B, A)routine.
OrdinaryHasInstance(C, O) routine performs the actual structural validation and prototype traversal:
- Bound Function Unwrapping: If
Cis a bound function (created viaFunction.prototype.bind), it lacks aprototypeproperty. The engine extracts its internal[[BoundTargetFunction]]and recursively callsInstanceofOperator(O, [[BoundTargetFunction]]). This ensures bound functions are evaluated against their original target function’s prototype. - Primitive Operand Check: The engine checks the type of
O. IfOis not an object (i.e., it is a primitive), the routine immediately returnsfalse. - Prototype Property Validation: The engine retrieves the
prototypeproperty ofC. IfC.prototypeis not an object (e.g., it isundefinedon an arrow function, or explicitly set to a primitive), the engine throws aTypeError. - Chain Traversal: The engine retrieves the prototype of
Ousing the internal[[GetPrototypeOf]]method (conceptually identical toObject.getPrototypeOf(O)). It recursively compares each prototype in the chain againstC.prototypeusing strict reference equality. - Termination: If a match is found, it returns
true. If the traversal reaches the end of the prototype chain (null) without a match, it returnsfalse.
Technical Nuances
Bound Functions Because theOrdinaryHasInstance routine unwraps bound functions before evaluating the prototype property, instanceof works seamlessly with them despite the absence of a prototype object on the bound function itself.
Symbol.hasInstance method, the instanceof operator delegates the evaluation entirely to it. This allows non-callable objects to act as the right-hand operand without throwing a TypeError.
instanceof operator does not trigger auto-boxing for primitive values. During the OrdinaryHasInstance routine, if the left-hand operand is a primitive (string, number, boolean, symbol, bigint, null, or undefined), the operator returns false. However, because Symbol.hasInstance is evaluated before the primitive check, custom implementations can successfully return true for primitives.
prototype property, the engine throws a TypeError during the OrdinaryHasInstance routine. This commonly occurs with arrow functions, which do not possess a prototype property.
instanceof behavior relies on strict reference equality, an object instantiated in Realm A will return false when evaluated against the corresponding constructor in Realm B.
Master JavaScript with Deep Grasping Methodology!Learn More





