First at all, let’s recap
The information in this table may vary depending on the author
- Why are the arrays not in the list?
- Why if we do
typeof arraydoes it return
typeof null returns
It is a historical quirk that the typeof operator returns
And a better explanation of this, was written by Dr. Axel Rauschmayer:
- 000: object. The data is a reference to an object.
- 1: int. The data is a 31 bit signed integer.
- 010: double. The data is a reference to a double floating point number.
- 100: string. The data is a reference to a string.
- 110: boolean. The data is a boolean.
That is, the lowest bit was either one, then the type tag was only one bit long. Or it was zero, then the type tag was three bits in length, providing two additional bits, for four types.
To check if a value is null, you can use a strict equality comparison (===) with the null value, like this:
const myValue = null; console.log(typeof myValue); // "object" console.log(myValue === null); // "true"
Why are the arrays not in the list? and Why if we do
typeof array does it return
When applied the typeof operator to an array returns the string
"object" for arrays because they are still implemented as objects in the language.
To check if a variable is an array, you can use the
Array.isArray() method, which returns
true if the given variable is an array, and
false otherwise. For example:
const myArray = [3, 6, 9]; console.log(typeof myArray); // "object" console.log(Array.isArray(myArray)); // "true"
About Andrés Bedoya
Learn more about Andrés Bedoya