// instanceof narrows to the specific class, enabling access to class-specific properties
Note instanceof checks the prototype chain at runtime. It does not work with interfaces or type aliases (they have no runtime presence). It also fails across different realms (iframes) since each realm has its own constructor.
Frequently asked questions
How do you check class type?
This task is covered in 2 stacks on this page: JavaScript, TypeScript. The "instanceof and Type Checking" snippet in JavaScript uses `object instanceof ClassName`.
Which code does the JavaScript example use?
The "instanceof and Type Checking" snippet uses `object instanceof ClassName`, from the Classes & OOP section of the JavaScript cheat sheet.
Which stacks cover "check class type" on this page?
JavaScript, TypeScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "instanceof and Type Checking": Checks the prototype chain. Can be unreliable across iframes or realms. For built-in types, prefer Array.isArray() over instanceof Array.