instanceof
noun
say it: “IN-stunss uv” /ˈɪnstəns ʌv/
An operator that answers true or false: was this object made from this class, or from a class that extends it?
What is instanceof in JavaScript?
instanceof — An operator, read "is an instance of", that gives true when an object was made from a class or from one that extends it. A catch block uses it to tell your own error classes apart.
class ServingsError extends Error {}
const problem = new ServingsError('Servings must be at least 1');
console.log(problem instanceof ServingsError); // true
console.log(problem instanceof Error); // true
In a sentence
- “In the catch block I checked err instanceof ServingsError to show a friendly message for that one problem.”
Level: Apprentice · More JavaScript terms at this level
Updated