typeof null === "object"
Level: Beginner
The typeof
operator is used to identify the basic type of a value in JavaScript. It can individually identify primitive types as well as recognize any other object type as being an object (with a special case for recognizing function objects as functions).
console.log(typeof 1); // number
console.log(typeof "text"); // string
console.log(typeof undefined); // undefined
console.log(typeof {}); // object
console.log(typeof function () {}); // function
// ...
One particular quirk with typeof
is that it will report the type of the value null
as “object”.
console.log(typeof null); // object
Because of this, if you’re using typeof
to check for objects in your code, you may also want to include a check to make sure it is not also null
. Otherwise, you might be accidentally using a null
value as an object in a way that could cause errors.
if (typeof someValue === "object" && someValue !== null) {
// I don't object to the use of this object
}
More info: