Preface: In daily development, we often encounter situations where we need to determine the data type of a variable or whether the variable is null. How do you choose the operator to determine the type? This article summarizes and records the knowledge points about 1. typeof operator (1) The return value is a string type, where:
(2) Common usage methods console.log(typeof undefined); //'undefined' console.log(typeof true);//'bpplean' console.log(typeof ("number")); //'string' console.log(typeof "number"); //'string' console.log(typeof 1);//'number' console.log(typeof Symbol());//'symbol' //For special objects such as Array and Null, typeof will always return object, which is the limitation of typeof console.log(typeof null); //'object' console.log(typeof [1, 2, 3]);//'object' console.log(typeof undefined); //'undefined' //Use the typeof operator to distinguish functions from other objects function f1() { console.log(111); } console.log(typeof f1); //'function' console.log(typeof f1()); // 111 'undefined' (3) You cannot use typeof to determine whether a variable exists var a; if (a === undefined) { console.log("variable does not exist") } else { console.log("variable exists") } // The variable does not exist 2. instanceof operator While function f1() { console.log(111); console.log(f1 instanceof Object);//true console.log(f1 instanceof Function);//true console.log(f1 instanceof RegExp); //false All reference values are instances of
function myInstanceof(left, right) { let proto = Object.getPrototypeOf(left), // Get the prototype of the object prototype = right.prototype; // Get the prototype object of the constructor function // Determine whether the prototype object of the constructor function is on the prototype chain of the object while (true) { if (!proto) return false; if (proto === prototype) return true; proto = Object.getPrototypeOf(proto); } } 3. The difference between typeof and instanceof and suggestions for use in development Both
It can be seen that the above two methods have disadvantages and cannot meet the needs of all scenarios. If you need to detect data types in general, it is recommended to use console.log(Object.prototype.toString.call(undefined)) //"[object Undefined]" console.log(Object.prototype.toString.call(true)) // "[object Boolean]" console.log(Object.prototype.toString.call('1')) // "[object String]" console.log(Object.prototype.toString.call(1)) // "[object Number]" console.log(Object.prototype.toString.call(Symbol())) // "[object Symbol]" console.log(Object.prototype.toString.call({})) // "[object Object]" console.log(Object.prototype.toString.call(function () { })) // "[object Function]" console.log(Object.prototype.toString.call([])) //"[object Array]" console.log(Object.prototype.toString.call(null)) //"[object Null]" console.log(Object.prototype.toString.call(/123/g)) //"[object RegExp]" console.log(Object.prototype.toString.call(new Date())) //"[object Date]" Summarize: This is the end of this article about the difference between You may also be interested in:
|
<<: Solution to navicat automatically disconnecting from the database after a period of time
>>: HTML tutorial, HTML default style
What is a file system We know that storage engine...
In the field of design, there are different desig...
Table of contents 1. Introduction 2. Environmenta...
It has always been very difficult to achieve wave...
Preface When developing a gateway project, the si...
Preface The default database file of the MySQL da...
1. Spread Operator The spread operator is three d...
MySQL password modification example detailed expl...
Table of contents Overview What is Image Compress...
This article shares the specific code of NodeJS t...
If you want to adjust the size and number of Inno...
React Native can develop iOS and Android native a...
Recently, the client of a project insisted on hav...
Recently, new projects have used springcloud and ...
Table of contents 1. Simple retrieval of data 2. ...