This is the 19th day of my participation in the Genwen Challenge

preface

We often have to judge the data type in the code, such as passing parameters, back parameters and so on, today we have a good talk about several ways to judge the JS data type.

The data type

Before we talk about the way to determine the js data type, let’s look at what the data type is:

Js has two types, basic type and reference type.

Basic types: String, Number, Boolean, Undefined, Null, Symbol (newly introduced type), BigInt (newly introduced type) Object(others such as Array, Function, etc. belong to the Object type)

Judgment way

Judgment way with three kinds of commonly, have typeof, instanof, Object. The prototype. ToString. Let me go through them one by one.

typeof

Returns a string representing the type of the detected element.

Grammar:

Typeof determines the element

Attached is an example of a typeof judgment type:

console.log(typeof 'the answer cp3') // 'string'
console.log(typeof 123) // 'number'
console.log(typeof true) // 'boolean'
console.log(typeof a) // 'undefined'
var b = null
console.log(typeof b) // 'object' is not null
console.log(typeof Symbol('the answer cp3')) // 'symbol'
console.log(typeof 123n) // 'bigint'
console.log(typeof {}) // 'object'
console.log(typeof []) // 'object'
console.log(typeof function c () {}) // 'function'
console.log(typeof new String('the answer cp3')) // 'object'
Copy the code

As can be seen from the above examples

  • typeofThe basic type judgment is basically accurate, but except for NULL, it returnsobject(This is a feature left over from history)
  • typeofIn the reference type judgment is not very accurate, because[]Returns theobjectWe would prefer to returnarray.New String(' answer cp3')Returns theobjectWhat we expect is thatstring

I usually use typeof more for basic type judgment, you can according to their own needs.

instanceof

This is used to determine whether an instance is an instance constructed by a constructor. Return true to indicate yes, false to indicate no

Grammar:

Determines the element instanceof constructor

Here is an example of the instanceof judgment type:

var arr = []
console.log(arr instanceof Array) // true
var obj = {}
console.log(arr instanceof Object) // true

function Dog () {}
var dogA = new Dog()
console.log(dogA instanceof Dog) // true
Copy the code

/ / return true if the __proto attribute of the instance is not equal to the prototype attribute of the constructor. / / Return true if the prototype attribute is equal to the __proto attribute of the instance. / / Return true if the prototype attribute is equal to the __proto attribute of the instance.

But the __proto and Prototype attributes can be changed, and if someone else does, you might get false

Here are some examples:

function Dog () {}
var dogA = new Dog()
Dog.prototype = {} // Change prototype
console.log(dogA instanceof Dog) // false
Copy the code

Object.prototype.toString

Typeof and Instanceof have their own advantages and disadvantages. Is there a way to solve every problem, to judge every type?

At that time, the Object. The prototype. The toString method.

Grammar:

Object. The prototype. ToString. Call (judgment)

Returns a string representing the internal data type of the judged element in Object

console.log(Object.prototype.toString.call('the answer cp3')) // [object String]
console.log(Object.prototype.toString.call(123)) // [object Number]
console.log(Object.prototype.toString.call(true)) // [object Boolean]
var a
console.log(Object.prototype.toString.call(a)) // [object Undefined]
var b = null
console.log(Object.prototype.toString.call(b)) // [object Null]
console.log(Object.prototype.toString.call(Symbol('the answer cp3'))) // [object Symbol]
console.log(Object.prototype.toString.call(123n)) // [object BigInt]
console.log(Object.prototype.toString.call({})) // [object Object]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(function c () {}))// [object Function]
console.log(Object.prototype.toString.call(new String('the answer cp3'))) // [object String]
Copy the code

This method is highly recommended and can be used for almost all data types.

conclusion

Above is my summary of some kinds of modes of js data type judgment, typeof, instanof, Object. The prototype. ToString, each has its own applicable scenario, everyone according to their own needs to choose what kind of way.

Thank you for reading and I hope it helped you understand