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)
A few days ago, the library said that the server ...
There is a table student in the mysql database, i...
There are many servers that can host static websi...
Let's first talk about the value of web front...
For the beginner's first installation of MySQ...
The complete syntax of the select statement is: S...
Effect To implement HTML, first prepare a clean H...
Table of contents Spring Boot Docker spring-boot-...
To write a drop-down menu, click the button. The ...
Table of contents Function Introduction function ...
Scenario Description In a certain system, the fun...
1. List The list ul container is loaded with a fo...
Table of contents Preface 1. The process of using...
Let's first look at the basic syntax of the c...
This article uses examples to illustrate the comm...