Check if instance is of a type
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether an instance is of a given type is a basic operation in many languages, but the exact tool depends on the language and on whether inheritance should count. The important distinction is usually not just "what syntax do I use," but "am I checking the exact runtime type or whether the object is compatible with a broader parent type or interface."
Python Uses isinstance
In Python, the standard answer is isinstance.
isinstance returns True for subclasses too, which is what you want most of the time.
Java Uses instanceof
Java uses the instanceof operator.
This is especially useful when dealing with polymorphic values stored under a base type reference.
C# Uses is
In C#, the common operator is is.
Modern C# also lets you combine the type check with pattern matching, which often reads better than checking and casting separately.
JavaScript Is More Context-Dependent
In JavaScript, instanceof works for class and constructor relationships, but not for primitive values in the same way developers from other languages may expect.
For primitives, typeof is often the correct tool instead.
Exact Type Versus Assignable Type
This is the conceptual trap behind many type-check questions. In most languages:
- instance compatibility checks include subclasses
- exact runtime type checks are stricter
For example, in Python type(obj) is SomeClass is stricter than isinstance(obj, SomeClass). Similar distinctions exist in other languages too.
Choose the strict version only when you really need exact-type behavior.
Prefer Polymorphism Over Repeated Type Checks
If your code is full of if chains that ask what type something is, that can be a design smell. In object-oriented code, polymorphism is often a better answer than manual type branching.
Type checks still have legitimate uses, especially at boundaries, deserialization points, or framework integration layers. The goal is not to ban them, but to use them where they clarify behavior instead of compensating for weak design.
Test Interface Compatibility When That Is the Real Requirement
Sometimes the real question is not whether an object is one concrete class, but whether it satisfies an interface or base contract. Checking against the abstraction is usually more robust than checking the most specific type.
Common Pitfalls
- Confusing exact type checks with inheritance-aware instance checks.
- Using JavaScript
instanceofwheretypeofis the correct primitive check. - Repeating many type checks when polymorphism would simplify the design.
- Forgetting that interfaces and base classes often count as valid matches.
- Assuming syntax from one language maps directly to another.
Summary
- Python uses
isinstance, Java usesinstanceof, and C# usesis. - Most instance checks include subclasses or compatible parent types.
- Exact runtime type checks are stricter and should be used deliberately.
- JavaScript needs extra care because
instanceofandtypeofsolve different problems. - Type checks are useful, but too many of them can signal a design issue.

