
typeof and instanceof operators are both used to determine data types, but their usage scenarios are different, and some details require special attention. Let's find out next, thoroughly master this knowledge point, and no longer be afraid of the interviewer's questions.
typeof is a unary operator placed in front of an operand, which can be of any type. It returns a string describing the type of the operand. Please look at the chestnut:
const type = typeof 'Long Live China'; // string
typeof 666; //number
typeof true; // boolean
typeof undefined; // undefined
typeof Symbol(); // symbol
typeof 1n; // bigint
typeof () => {}; // function
typeof []; // object
typeof {}; // object
typeof new String('xxx'); // object
typeof null; // object As can be seen from the above examples, typeof can only accurately determine basic data types and functions (functions are actually objects and do not belong to another data type, but they can also be distinguished using typeof) and cannot be accurately determined. Out reference data type (all return object).
One thing to note is that calling typeof null returns object . This is because the special value null is considered a reference to a null object (also called a null object pointer).
If you want to accurately determine the reference data type, you can use instanceof operator.
The instanceof operator is placed after an operand and before the given object. It returns a Boolean value indicating whether the operand is an instance of the given object:
const result = [] instanceof Array; // true
const Person = function() {};
const p = new Person();
p instanceof Person; // true
const message = new String('xxx');
message instanceof String; // true typeof will return the basic type of an operand, instanceof returns a Boolean value
instanceof can accurately determine the reference data type, but cannot correctly determine the basic data type
typeof although it can determine the basic data type (except null) , but it is impossible to determine the reference data type (except function)
typeof and instanceof have certain drawbacks and cannot meet the needs of all scenarios. If you need to universally detect data types, you can use Object.prototype.toString.call() method:
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call(666); // "[object Number]"
Object.prototype.toString.call('xxx'); // "[object String]" Note that this method returns a string in the format of "[object Object]" .
For more convenient use, we can encapsulate this method:
function getType(value) {
let type = typeof value;
if (type !== 'object') { // If it is a basic data type, return directly return type;
}
// If it is a reference data type, further judgment is made, and the regular return result is return Object.prototype.toString.call(value).replace(/^[object (S+)]$/, '$1');
}
getType(123); // number
getType('xxx'); // string
getType(() => {}); // function
getType([]); // Array
getType({}); // Object
getType(null); // Null