JavaScript typeof
The typeof operator in JavaScript is used to ascertain the data type of a value or expression. The type of the operand is represented by a string that the typeof operator returns.
There are 5 distinct data types in JavaScript that may hold values:
- string
- number
- boolean
- object
- function
There are 6 different kinds of objects:
- Object
- Date
- Array
- String
- Number
- Boolean
There are only two data types that are valueless.
- null
- undefined
The typeof Operator
The typeof operator can be used to determine a JavaScript variable's data type.
Primitive Data
A single basic data item without any extra characteristics or methods is referred to as a primitive data value.
One of these basic kinds can be obtained by using the typeof operator:
- string
- number
- boolean
- undefined
Complex Data
One of two complex types can be returned by the typeof operator:
- function
- object
In the case of objects, arrays, and null, the typeof operation returns "object". For functions, the typeof operator does not return "object".
The Data Type of typeof
An operator's type is not a variable. An operator, that is. There is no data type for the operators (+ - * /). The typeof operation, however, always yields a string (containing the type of the operand).
The constructor Property
All JavaScript variables' constructor functions are returned through the constructor property. The constructor attribute is used to determine if an object is a Date or an Array (contains the word "Array") (contains the word "Date").
Undefined
A variable in JavaScript has the value undefined if it doesn't have a value. Furthermore, the type is unspecified. By changing the value to undefined, any variable may be cleared. Moreover, the kind will be unclear.
Empty Values
Undefined has nothing to do with an empty value. An empty string has a type and a lawful value.
Null
Null is "nothing" in JavaScript. It is meant to represent something that does not exist. Regrettably, null's data type in JavaScript is an object. Setting an object to null and undefined will empty it.
Difference Between Undefined and Null
Null and undefined have the same value but are of distinct types.
The instanceof Operator
If an object is an instance of the supplied object, the instanceof operator returns true.
The void Operator
After evaluating an expression, the void operator returns undefined. With "void(0)," this operator is frequently used to acquire the undefined primitive value (useful when evaluating an expression without using the return value).