Use instanceof for custom types:

var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance; // object
typeof instance == 'ClassFirst'; // false
instance instanceof Object; // true
instance instanceof ClassFirst; // true
instance instanceof ClassSecond; // false 

Use typeof for simple built in types:

'example string' instanceof String; // false
typeof 'example string' == 'string'; // true

'example string' instanceof Object; // false
typeof 'example string' == 'object'; // false

true instanceof Boolean; // false
typeof true == 'boolean'; // true

99.99 instanceof Number; // false
typeof 99.99 == 'number'; // true

function() {} instanceof Function; // true
typeof function() {} == 'function'; // true

Use instanceof for complex built in types:

/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; // object

[] instanceof Array; // true
typeof []; //object

{} instanceof Object; // true
typeof {}; // object

And the last one is a little bit tricky:

typeof null; // object
Answer from Szymon Wygnański on Stack Overflow
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-typeof-and-instanceof-in-javascript
What is the difference between typeof and instanceof in JavaScript · CoreUI
September 17, 2024 - Note: Both arrays and objects return 'object' when using typeof. To differentiate between them, you might need additional checks. The instanceof operator checks if an object is an instance of a specific class or constructor function.
Discussions

Difference Between typeof and instanceof in JavaScript | Interview Question
JS gives you 2 keywords to verify the type of the value referred to by a reference. First of all, you have the typeof keyword. This keyword, used in the form of typeof will return a string that takes values from the following list: number string undefined object undefined symbol bigint boolean As we can see, typeof returns a primitive type. An interesting corner case is typeof null === 'object' surprisingly. Every class instance will also return 'object' on a typeof call. If you want to test for a specific class type, you have to use the instanceof keyword. The syntax is instanceof . What the aforementioned expression will return is a boolean value - true/false, if the class is part of the prototype chain. const m = new Map(); console.log(m instanceof Map) // true console.log(m instanceof Set) // false class MyMap extends Map { } const mm = new MyMap(); console.log(mm instanceof Map) // true console.log(mm instanceof MyMap) // true console.log(mm instanceof Set) // false More on reddit.com
🌐 r/Angular2
1
1
September 30, 2022
typescript - Typeof/instanceof type alias - Stack Overflow
I wonder if it is possible to determine the type of an object in typescript. Please consider the example below: type T = [number, boolean]; class B { foo: T = [3, true]; bar(): boolean { return this.foo instanceof T; } } The typeof operator does not seem to be a solution, neither does instanceof. More on stackoverflow.com
🌐 stackoverflow.com
If typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? And how can we take advantage of this functionality in our typing?
instanceof tests *at runtime*, not at compile time, and it tests towards the *class* Error, and not the *interface* Error. Both exists and are defined in TS. This kind of situations is a bit of a mess, because a class is both a type at build time and a value at runtime (classes are just functions). More on reddit.com
🌐 r/typescript
27
0
October 23, 2024
type .kind checks vs. class instanceof checks
It looks like you're using the same example as the official documentation on Discriminated Unions , so I assume you've seen that section. Out of curiosity then, what makes you say this? However newer docs don't seem to ever show discriminated unions If you're looking to have discriminated unions like Rust, follow that section to roll your own or look for a library that's already implemented them. fp-ts has everything under the sun in a Haskell style, neverthrow has Rust-like Result and ResultAsync, and sum-types helps with the boilerplate of defining your own sum types. I wouldn't recommend instanceof checks, TypeScript just isn't designed to work well with nominal typing so it's not the best experience. More on reddit.com
🌐 r/typescript
21
24
May 8, 2022
🌐
Thisthat
thisthat.dev › instanceof-vs-typeof
instanceof vs typeof - this vs that - Phuoc Nguyen
'helloworld' instanceof String; // false new String('helloworld') instanceof String; // true · If you want to check if a value is a primitive string or a String object, then you need to use both operators: const isString = (value) => typeof value === 'string' || value instanceof String; isString('helloworld'); // true isString(new String('helloworld')); // true
🌐
2ality
2ality.com › 2017 › 08 › type-right.html
Beyond `typeof` and `instanceof`: simplifying dynamic type checks
August 18, 2017 - This blog post describes a technique ... have to choose when it comes to checking the type of a value. The rough rule of thumb is: typeof checks if a value is an element of a primitive type:...
🌐
Bambielli's Blog
bambielli.com › til › 2017-06-18-typeof-vs-instanceof
instanceof vs. typeof - Bambielli’s Blog
June 18, 2017 - const a = "I'm a string primitive"; const b = new String("I'm a String Object"); a instanceof String; --> returns false b instanceof String; --> returns true · In the case of the puzzle I was trying to solve, I had to detect both primitive strings and String objects as values of the object in question’s keys. To account for both primitives and objects, I came up with the following function: const isString = (str) => { return typeof str === 'string' || str instanceof String }
Find elsewhere
🌐
DEV Community
dev.to › imranmind › javascript-interview-trick-typeof-vs-instanceof-3jlb
JavaScript Interview Trick: typeof vs instanceof - DEV Community
April 29, 2025 - Use 'typeof' when you want to know the type of a primitive value. Use 'instanceof' when you want to know if an object is an instance of a certain class.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › typescript instanceof type guard
Typescript Instanceof Type Guard - Tektutorialshub
March 15, 2023 - On the other hand, instanceof is applicable only to objects and does not work on primitive values. instanceof will return true only if it matches the function from which it was constructed.
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-typeof-instanceof
Type checking in JavaScript: typeof and instanceof operators
November 24, 2020 - JavaScript is a loosely-typed language, ... you have to check what type the variable has. typeof expression is the operator that lets you determine the type of expression....
🌐
Reddit
reddit.com › r/angular2 › difference between typeof and instanceof in javascript | interview question
r/Angular2 on Reddit: Difference Between typeof and instanceof in JavaScript | Interview Question
September 30, 2022 - As we can see, typeof returns a primitive type. An interesting corner case is typeof null === 'object' surprisingly. Every class instance will also return 'object' on a typeof call. If you want to test for a specific class type, you have to use the instanceof keyword...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript - MDN Web Docs
July 8, 2025 - class C { #value = "foo"; static [Symbol.hasInstance](x) { return this === C && #value in x; } } class D extends C {} console.log(new C() instanceof D); // false console.log(new C() instanceof C); // true console.log({ __proto__: C.prototype } instanceof C); // false · typeof ·
🌐
Webdevtutor
webdevtutor.net › blog › typescript-typeof-vs-instanceof
Understanding TypeScript's `typeof` and `instanceof` Operators
You could use typeof to determine the type of an object, and then use instanceof to check if that object is an instance of a specific class.
🌐
EyeHunts
tutorial.eyehunts.com › home › what is the difference between typeof and instanceof?
What is the difference between typeof and instanceof?
September 21, 2023 - The typeof is a unary operator that returns a string indicating the type of the unevaluated operand. Where instanceof is a binary operator, that accepts an object and a constructor.
🌐
Medium
medium.com › @AlexanderObregon › the-mechanics-behind-javascripts-typeof-and-instanceof-3299ff38f20a
The Mechanics of JavaScript’s typeof and instanceof | Medium
May 16, 2025 - That’s why typeof returns "object" instead of "string" here. This kind of thing doesn't happen often in modern code, but it can show up in older codebases or libraries. You can usually avoid boxed primitives altogether. Most of the time, they only create confusion without much benefit. The instanceof operator looks like a way to ask if something was created by a particular constructor.
🌐
Medium
medium.com › @seanbridger › advanced-typescript-7-10-guarding-types-with-typeof-instanceof-literals-c46c2668df45
Advanced TypeScript (7/10): Guarding Types with typeof/instanceof/literals | by Likely Coder | Medium
November 24, 2023 - function logMessage(message: string ... console.log(message.toFixed(2)); } } The instanceof operator checks if an object is an instance of a particular class or constructor function....
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › instanceof: understanding javascript object types
instanceof: Understanding JavaScript Object Types, by John Kavanagh
June 1, 2024 - Use instanceof if you want a simple yes/no answer to the question "Is this variable an instance of xyz?". Use typeof if you want an answer to the question "What type is my variable?
Price   $$
Address   66 Paul Street, EC2A 4NE, London
🌐
Reddit
reddit.com › r/typescript › if typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? and how can we take advantage of this functionality in our typing?
r/typescript on Reddit: If typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? And how can we take advantage of this functionality in our typing?
October 23, 2024 -

Edit: I know that instanceof checks at runtime, I'm very familiar with it and have been using it for many years. My question is specifically how the typing system works in typescript, how/why typescript is able to discern via type inference the difference between Error and PseudoError. And, given that it can, whether it is possible to leverage that kind of discernment to create a utility function that's based on this rather than based on shapes and interfaces.

In lib.es5.d.ts in typescript we have:

interface Error {
    name: string;
    message: string;
    stack?: string;
}

Now in my typescript file, if I create a new type, like this:

class PseudoError {
    name = "hello"
    message = "world"
    stack? = "friends"
}

And I do the following:

const test : PseudoError | Error = new PseudoError()

type withoutError = Exclude<typeof test, Error> // <--- results to never, because of duck typing ?

The above is expected, because PseudoError and Error implement the same interface. So if I'm excluding Error, then I'm also excluding PseudoError by duck type standards.

But when I do this:

if (test instanceof Error) {
  test // <--- hover over = Error
} else {
  test // <--- hover over = PseudoError
}

It suggests that there is obviously some built-in functionality in typescript that doesn't work like in a duck-type-y way. This clearly aligns with the desired outcome in JS, but in my situation, I would like to have an Exclude<...> utility type that does the same thing as what typescript is doing behind the scenes with instanceof.

For example, where's this NotInstanceOf utility function?

const test : PseudoError | Error = new PseudoError()

type withoutError = NotInstanceOf<typeof test, Error> // <--- results to PsuedoError
🌐
Convex
convex.dev › typescript 101 › control flow & operators › instanceof operator
Instanceof Operator | TypeScript Guide by Convex
Unlike typeof, which only reveals basic types like "object" or "string", instanceof traverses the entire prototype chain. This makes it perfect for working with inheritance hierarchies in object-oriented code.