JavaScript typeof Operator
The typeof javascript operator returns a string that indicates the type of the operand’s value.
SYNTAX – typeof operand
Operand – An expression which is representing the object or primitive data whose type is to be returned.
The type of method can return one of these primitive data types ( A primitive data value can be used to refer to a single simple data value with no extra or additional properties and methods.)
- Number
- Boolean
- String
- Undefined
It can also be used to return complex data types like functions or objects. It returns “object” for objects, arrays, and null and does not return “object” for functions.
NOTE – The typeOf() operator returns objects for arrays because, in JavaScript, arrays are treated as objects.
Data Type of typeOf() ?
typeOf() is an operator in JavaScript, data types are defined for variables, but not for operators like ( +,-,*,/)
Why NULL is considered as objects?
In the first implementation of JavaScript, its values were represented as a type tag and a value. 0 was the type tag for objects and null was represented as the NULL pointer. And hence, null had 0 as the type tag, hence the typeof return value “object”.
For Example