typeof is an operator, and the result it returns to the operand is a string, with 6 types (only for ES, not HOST environment objects).
1.'undefined'
2.'boolean'
3.'string'
4.'number'
5.'object'
6.'function'
Because it is an operator itself and it is not a function, there is no need to add brackets when using it.
The code copy is as follows:
if (typeof(obj) === 'undefined') {
// ...
}
Typeof is used to judge types, and it has several pitfalls
1. Returning null is 'object', but you cannot really use it as an object.
The code copy is as follows:
var obj = null
if (typeof obj === 'object') {
obj.a() // An error is reported here
}
2. Returning NaN is 'number', but you cannot use it to perform arithmetic operations.
The code copy is as follows:
var obj = {}
var num = parseInt(obj.a)
if (typeof num === 'number') {
num = num + 10 // num is still NaN after execution
}
3. It cannot distinguish between objects, arrays, and regular operations, and returns 'object' for all operations.
The code copy is as follows:
var obj = {}
var arr = []
var reg = /pop/g
console.log(typeof obj) // 'object'
console.log(typeof arr) // 'object'
console.log(typeof reg) // 'object'
4. Versions before Safar5 and Chrome7 return 'function' to regular objects.
Finally, the explanation in the specification is posted