This article summarizes four judgment methods: 1. typeof // string console.log(typeof('lili')); // string // number console.log(typeof(1)); // number // Boolean value console.log(typeof(true)); // boolean // undefined console.log(typeof(undefined)); // undefined // object console.log(typeof({})); // object // array console.log(typeof([])); // object // null console.log(typeof(null)); // object // function console.log(typeof(() => {})); // function // Symbol value console.log(typeof(Symbol())); // symbol 2. instanceof object instanceof constructor const arr = [1, 2]; // Check if Object's prototype is in the prototype chain of the array console.log(arr instanceof Object); // true // Prototype of array arr const proto1 = Object.getPrototypeOf(arr); console.log(proto1); // [] // The prototype of the prototype of array arr const proto2 = Object.getPrototypeOf(proto1); console.log(proto2); // [] //Object's prototype console.log(Object.prototype); // Check if the prototype of arr is equal to the prototype of Object console.log(proto1 === Object.prototype); // false // Check if the prototype of arr's prototype is equal to Object's prototype console.log(proto2 === Object.prototype); // true 3. ConstructorThis judgment method actually involves the relationship between prototypes, constructors, and instances. A more in-depth explanation will be given later. Below you only need to briefly understand the relationship between these three. When defining a function (constructor), the JS engine will add a const val1 = 1; console.log(val1.constructor); // [Function: Number] const val2 = 'abc'; console.log(val2.constructor); // [Function: String] const val3 = true; console.log(val3.constructor); // [Function: Boolean] Although this method can determine its data type, it has two disadvantages:
4. toString() The results returned by this type for different variable types are as follows: It is easy to construct a type identification function using this method. The code is as follows: function type(target) { const ret = typeof(target); const template = { "[object Array]": "array", "[object Object]":"object", "[object Number]":"number - object", "[object Boolean]":"boolean - object", "[object String]":'string-object' } if(target === null) { return 'null'; } else if(ret == "object"){ const str = Object.prototype.toString.call(target); return template[str]; } else{ return ret; } } console.log(type({})); // object console.log(type(123)); // number console.log(type('123')); // string This concludes this article about the four data type judgment methods in JS. For more information about data type judgment methods in JS, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL paging query optimization techniques
>>: Solution to the problem that mixin does not work in scss (browser cannot compile it)
Definition of Float Sets the element out of the n...
Table of contents describe accomplish The project...
Refer to the official document http://dev.mysql.c...
Using the internal function instr in MySQL can re...
Problem Description Recently, a host reported the...
We implement a red image style for the clicked bu...
This article example shares the specific code of ...
<br />Most of the time when we talk about na...
Mysql8.0.12 decompression version installation me...
Preface We have already installed Docker and have...
For a website, usability refers to whether users c...
1. Compare the old virtual DOM with the new virtu...
Table of contents 1. Introduction 2. On-demand at...
1. Interconnection between Docker containers Dock...
Install mysql5.7.18 on CentOS6.7 1. Unzip to the ...