js data type classification and judgment (typeof, instanceof, Object.prototype.toString.call)

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

foreword

Record the type classification and judgment method of js, the usage of typeof and instanceof and the existing problems, and finally introduce the method of using Object.prototype.toString.call() to accurately judge the type.

js type

  1. There are many kinds of js 内置类型, of which built-in types are divided into two categories, 基本数据类型and 复杂==对象(Object)类型.

  2. Basic data types: Null, Undefined, Number, String, Boolean, Symbel, BigInt. (As of 2022/2/16, more basic data types may be introduced in the future, just like Symbel and BigInt are relatively new introductions, there are only five basic data types)

typeof

  1. Test the basic data type: Remember the conclusion, except that null will be judged as object , other basic types can be judged, so it cannot be used for judgment null.
    console.log(
       typeof null,// object
       typeof 123,//或 Number(123),// number
       typeof undefined,// undefined
       typeof "str",//或 String('str'),// string
       typeof true,//或 Boolean(true)// boolean
       typeof Symbol(1),// symbol
       typeof 12345678910n,//或 BigInt(12345678910),// bigint
     );
    复制代码
  2. Measure the types of complex objects such as groups, objects, built-in objects, new function declaration objects, functions, etc.: remember the conclusion, except that functions will be correctly judged as functions , all others are objects, so 除了函数之外complex types that cannot be used for judgment cannot use typeof differentiate.
    console.log(
      typeof [],//object
      typeof {},//object
      typeof Math,//object
      typeof new String(),//object
      typeof (() => {}),//function
    );
    复制代码

instanceof

  1. First of all, it is clear that instanceof cannot judge the basic data type, and cannot directly try to get the type of the content you want to judge, but can only try to judge whether it is a certain type .

  2. We can use it to determine whether it is a complex object type.

console.log(
    [] instanceof Array, //true
    {} instanceof Object, //true
    new Date() instanceof Date, //true
    (() => {}) instanceof Function, //true
)
复制代码
  1. 因为它的原理是原型链,所以存在一个问题,就是Object的原型作为原型链的终点,js的复杂类型本质也都是对象,因此判断他们是否是对象,也都为true。
console.log(
    [] instanceof Object, //true
    {} instanceof Object, //true
    new Date() instanceof Object, //true
    (() => {}) instanceof Object, //true
)
复制代码

精准判断

  • .想要精准判断类型,建议使用Object.prototype.toString.call()
     console.log(
       Object.prototype.toString.call(null), //[object Null]
       Object.prototype.toString.call("123"), //[object String]
       Object.prototype.toString.call(123), //[object Number]
       Object.prototype.toString.call(undefined), //[object Undefined]
       Object.prototype.toString.call(true), //[object Boolean]
       Object.prototype.toString.call(Symbol(1)), //[object Symbol]
       Object.prototype.toString.call(BigInt(1)), //[object BigInt]
       Object.prototype.toString.call(() => {}), //[object Function]
       Object.prototype.toString.call([]), //[object Array]
       Object.prototype.toString.call({}), //[object Object]
       Object.prototype.toString.call(Math), //[object Math]
       Object.prototype.toString.call(new String()) //[object String]
     );
    复制代码

尾言

如果觉得文章对你有帮助的话,欢迎点赞收藏哦,有什么错误或者意见建议也可以留言,感谢~

Guess you like

Origin juejin.im/post/7085531309459636255