Home >Web Front-end >JS Tutorial >Why Does `typeof(null)` Return 'object' in JavaScript?
In the context of JavaScript primitives, the question arises as to why the type operator (typeof) returns "object" for the value null. The logic of primitive data types suggests that a primitive value like null should not return "object".
The explanation for this inconsistency lies in the early implementation of JavaScript. When JavaScript values were represented by a type tag and a value, null was assigned a type tag of 0, similar to that of objects. This legacy representation became ingrained in JavaScript's behavior despite the existence of a proposed fix that would have changed typeof(null) to "null."
Therefore, although null is considered a primitive data type in JavaScript, the implementation holds a historical artifact that results in typeof(null) returning "object" to preserve backward compatibility with earlier versions of the language.
The above is the detailed content of Why Does `typeof(null)` Return 'object' in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!