The typeof operator

The first thing you'll often be exposed to when you first try to detect a type in JavaScript is the typeof operator:

typeof 1; // => number

The typeof operator accepts a single operand, to its right-hand-side, and will evaluate to one of eight possible string values, depending on the value that's passed:

typeof 1; // => "number"
typeof ''; // => "string"
typeof {}; // => "object"
typeof function(){}; // => "function"
typeof undefined; // => "undefined"
typeof Symbol(); // => "symbol"
typeof 0n; // => "bigint"
typeof true; // => boolean

If your operand is an identifier without a binding, that is, an undeclared variable, then typeof will usefully return "undefined" instead of throwing a ReferenceError like any other reference to that variable would do:

typeof somethingNotYetDeclared; // => "undefined"

typeof is the only operator in the JavaScript language that does this. Every other operator and every other way of referencing a value will throw an error if that value is not yet declared.

Outside of detecting undeclared variables, typeof is really only useful when determining primitive types—and even that's too generous since not all primitive types are detectable. A null value, for example, when passed to typeof, will evaluate to a rather useless "object":

typeof null; // => "object"

This is an unfortunate and unfixable legacy of the JavaScript language. It will likely never be fixed. To check for null, it is preferred to explicitly check for the value itself:

let someValue = null;
someValue === null; // => true

The typeof operator does not differentiate between different types of objects, except for functions. All non-function objects in JavaScript will return, plainly, "object":

typeof [];         // => "object"
typeof RegExp(''); // => "object"
typeof {}; // => "object"

All functions, whether declared via class definitions, method definitions, or plain function expressions, will evaluate to "function":

typeof () => {};          // => "function"
typeof function() {}; // => "function"
typeof class {}; // => "function"
typeof ({ foo(){} }).foo; // => "function"

If typeof class {} evaluating to "function" is confusing, consider that, as we've learned, all classes are just constructor functions with a prepared prototype (which will later determine the [[Prototype]] of any produced instances). There's nothing special about them. Classes are not a unique type or entity within JavaScript.

When it comes to comparing the result of typeof to a given string, we can use either the strict equality (===) or abstract equality (==) operator. Since typeof always returns a string, we don't have to worry about any discrepancies here, so whether you adopt a strict versus abstract equality check is up to you. These would both be fine, technically:

if (typeof 123 == 'number') {...}
if (typeof 123 === 'number') {...}
The strict and abstract equality operators (double-equals and triple-equals) behave slightly differently, although when the values on both sides of the operator are of the same type, they act identically. Skip ahead to the Operator section to get the lowdown on how they differ. In general, it's best to prefer === over ==.

In conclusion, the typeof operator is only a fair-weather friend. We cannot rely on it in all circumstances. Sometimes, we'll need to use other type detection techniques.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.105.137