- Typeof [value] An operator that detects data types
- [example] instanceof [class] Checks whether an instance belongs to this class
- [example].constructor === [class] Detect instance and class relationships to detect data types
- OBject. The prototype. ToString. Call ([value]) detection data types
Detail points for typeOf
- The result of typeof detection is first a string containing the corresponding data type (e.g., “number “string”, “Boolean”, “undefined”, “object”, “function”, “symbol”, “bigint”).
- Special test results:
- NaN/Infinity are all numbers and the result is “number”
- Typeof null results in “object”. The first three digits of null are 000, so they are recognized as objects. However, it is not an object. It is a null object pointer, which is a primitive value.
- Typeof Normal objects/array objects/regular objects… The result is “object”, which makes it impossible to distinguish between ordinary objects and array objects based on Typeof
console.log(typeof typeof typeof []); //=>" String "because the result of typeof is always a string (the string contains the corresponding type), the result of two or more consecutive Typeof detection is "string".Copy the code
// We know that there is a variable x, but we can not confirm its data type, we need to have a judgment operation: // if (typeof x == "object") {// if (typeof x == "object") {// if (typeof x == "object"); Or maybe null // //... // } if (x ! = null && typeof x == "object") { // ... }Copy the code