Classification of JS data types and common judgment methods

  • 2021-09-24 21:13:09
  • OfStack

Method of data type judgment

When exploring data type judgment methods, we need to know which data types are in JS:

We can divide the data types in JS into two categories:

1. Basic data types: Undefined, Null, Boolean, Number, String and Symbol (newly added in es6)

2. Reference type (complex data type): It contains function, Array, Date and so on

There are several ways to determine the data type

1.typeof

I believe that typeof, a method for judging data types, is commonly used by everyone. Without much gossip, it is directly on the code:

console.log(typeof 1);//number
console.log(typeof 'hello');//string
console.log(typeof true);//boolean
console.log(typeof null);//object
console.log(typeof Symbol(1));//symbol
console.log(typeof undefined);//undefined
console.log(typeof function(){});//function
console.log(typeof []);//object
console.log(typeof {});//object

From the above code and output, we can probably see that typeof can judge most data types, but null is judged as object among the basic types

So this judgment is not so accurate, and we need other judgment methods

2. instanceof is used to detect whether the prototype attribute of the constructor appears on the prototype chain of an instance

1. instanceof the left is the instance and the right is the constructor. That is, an instance object that judges whether left is right or not. The internal mechanism is judged by prototype chain

2. instanceof can accurately judge the reference data types Array, Function and Object, while the basic data type cannot be accurately judged by instanceof because it is not an instance object itself

console.log(2 instanceof Number);//false
console.log(new Number(2) instanceof Number);//true
console.log('str' instanceof String); //false
console.log(new String('str') instanceof String); //true
console.log([] instanceof Array);//true
console.log([]instanceof Object);//true
console.log({} instanceof Object);//true
console.log({} instanceof Array);//false
console. log (function () {} instanceof Function); //true

In the above code, we can see that this judgment method still does not solve the practical problem. For example, we want to judge null as null, so we will introduce an accurate judgment method next

3.Object.prototype.toString.call()

1. Use the prototype method of the Object object, toString, and use call to change the this pointing

See code:

const a = Object.prototype.toString;
console.log(a.call(2)); // [object Number]
console.log(a.call(true)); // [object Boolean]
console.log(a.call('str')); // [object String]
console.log(a.call(Symbol())) // [object Symbol]
console.log(a.call([])); // [object Array]
console.log(a.call(function(){})); // [object Function]
console.log(a.call({})); // [object Object]
console.log(a.call(undefined)); // [object Undefined]
console.log(a.call(null)); // [object Null]
console.log(a.call(new Date())) // [object Date]
console.log(a.call(new Error())) // [object Error]
console.log(a.call(new RegExp())) // [object RegExp

As can be seen from the above output, Object. prototype. toString. call () can judge Array, Function, Date and other types of complex types, so we know that when we need to judge complex types or judge null as null in simple data types, we can use secondary methods to judge other data types in simple types. We can use typeof to judge


Related articles: