background
The Typeof and Instanceof operators are both used to determine data types, but they are used in different ways, and some of the details require special attention. Let’s take a look, get the hang of it, and don’t be afraid to ask any more questions.
typeof
Typeof is a unary operator that precedes an operand that can be of any type. It returns a string indicating the type of operand. Look at the chestnuts:
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; // objectCopy the code
As you can see from the examples above, Typeof can only accurately determine basic data types and functions (functions are objects that are not of another data type, but can also be distinguished using Typeof), but not reference data types (all return object).
It is important to note that calling Typeof NULL returns object, because the special value null is considered a reference to an empty object (also known as a null object pointer).
If you want to determine exactly what the reference data type is, you can use the instanceof operator.
instanceof
The instanceof operator is placed after an operand and before an object. It returns a Boolean indicating whether the operand is an instance of an 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
Copy the code
The difference between
-
Typeof returns the primitive typeof an operand, and instanceof returns a Boolean value
-
Instanceof can accurately determine the reference data type, but not the base data type
-
Typeof can determine base data types (except null), but cannot determine reference data types (except function).
extension
Object.prototype.toString.call()
Typeof and Instanceof both have certain disadvantages and are not suitable for all scenarios. If you need general detection data type, you can use the Object. The 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]"
Copy the code
Notice that this method returns a string of the form “[object object]”.
Packaging function
For ease of use, we can encapsulate this method:
function getType(value) { let type = typeof value; if (type ! == 'object') {// If it is a basic data type, return type; } / / if it is the reference data types, and then further judgment, regular return results return Object. The prototype. ToString. Call (value). The replace (/ ^ \ [Object (\ S +) \] $/, "$1"); } getType(123); // number getType('xxx'); // string getType(() => {}); // function getType([]); // Array getType({}); // Object getType(null); // NullCopy the code
The last
If there are any mistakes or inadequacies in this article, you are welcome to correct them in the comments section.
Your thumbs-up is a great encouragement to me! Thanks for reading ~