Sub topic 17
Inspecting Values with typeof, instanceof, and in
This chapter assumes you know JavaScript's primitive types, how objects and arrays work, and that objects are compared by reference. It covers three operators that answer different questions about a value:
typeofasks what kind of primitive value this is, or whether it is a function.instanceofasks whether a constructor's prototype is in this object's prototype chain.inasks whether this object has a property with a given key, either its own or inherited.
typeof: A Type Tag as a String
typeof always returns a string. It never throws an error for an undeclared variable, so it is a safe way to check whether a global exists.
console.log(typeof 42); // 'number'
console.log(typeof 10n); // 'bigint'
console.log(typeof 'hi'); // 'string'
console.log(typeof Symbol()); // 'symbol'
console.log(typeof undefined); // 'undefined'
console.log(typeof null); // 'object' (a long-standing quirk)
console.log(typeof []); // 'object'
console.log(typeof function () {}); // 'function'
console.log(typeof notDeclared); // 'undefined', no ReferenceErrorThe common mistake is to write typeof x === 'object' and assume x is a usable object. That test also passes for null and for arrays. Check for null explicitly, and use Array.isArray to detect arrays.
instanceof: Walking the Prototype Chain
obj instanceof C checks whether C.prototype appears anywhere in the prototype chain of obj. It does not check how the object was created, and it always returns false for primitives.
class Animal {}
class Dog extends Animal {}
const d = new Dog();
console.log(d instanceof Dog); // true
console.log(d instanceof Animal); // true, inherited
console.log(d instanceof Object); // true
console.log('text' instanceof String); // false: primitives have no chain
console.log(Object.create(null) instanceof Object); // false: empty chain
console.log(Array.isArray([])); // trueTwo failure modes come up often. First, objects from another realm, such as an iframe or a Node vm context, have a different Array.prototype, so instanceof Array returns false for them. Array.isArray works across realms. Second, if you reassign a constructor's prototype after objects were created, old instances stop matching.
in: Does the Key Exist?
key in obj returns true if the property exists, even when its value is undefined. It also finds inherited properties. If you only want the object's own properties, use Object.hasOwn.
const config = { debug: undefined };
console.log('debug' in config); // true
console.log(config.debug !== undefined); // false: a different question
console.log('toString' in config); // true, inherited from Object.prototype
console.log(Object.hasOwn(config, 'toString')); // false
console.log(0 in ['a']); // true: array index 0 existsThe right-hand side must be an object. 'length' in 'abc' throws a TypeError.
| Question | Tool |
|---|---|
| Is it a primitive of type X? | `typeof` |
| Is it an array? | `Array.isArray` |
| Was it built from class C? | `instanceof` |
| Does the key exist, inherited or own? | `in` |
| Does the key exist on the object itself? | `Object.hasOwn` |
Exercise
What do these log: typeof null, [] instanceof Object, and 'x' in Object.create({ x: 1 })?
Show answer
'object', true (Array.prototype inherits from Object.prototype), and true (in finds the inherited property x).
Exercise
Write a check that is true only for non-null, non-array objects.
Show answer
value !== null && typeof value === 'object' && !Array.isArray(value)