What are the methods for JS data type detection
What are the data types in JavaScript
In JavaScript, data types are divided into primitive and object types
Raw data types include
- String: Represents a string, for example:'jingzhi'
- Number: Represents a numeric value, for example, 123
- Boolean: Represents a Boolean type of data with two values true and false
- Null: Indicates a reference to an object or address that does not exist or is invalid
- Undefined: The state of a variable when it is not assigned, indicating that it is undefined
- Symbol: Declares a symbol that is unique and cannot participate in operations, for example: const a = Symbol('a')
Reference data types include - Object: Represents an object, for example: var a = {}
- Function: Represents a function, such as function foo() {}
- Array: Represents an array, for example: [1, 2, 3, 4, 5]
- There are many more left, such as Date, Math, Set, Map, and so on
What are the ways to detect data types in JavaScript?
typeof
typeof 'a' // 'string' typeof 1 // 'number' typeof NaN // number typeof true // 'boolean' typeof undefined // 'undefined' typeof null // 'object' typeof Symbol('a') // 'object' typeof {} // 'object' typeof [] // 'object' typeof /a/ // 'object' typeof function() {} // 'function' ....
Conclusion:
1.You can see that typeof can only detect raw data types other than null
2.Typeeof can only judge Function s in Object types, the rest are Objects
instanceof
console.log(1 instanceof Number) // false console.log(new Number(1) instanceof Number) // true const arr = [] console.log(arr instanceof Array) // true console.log(arr instanceof Object) // true function Fn() { this.name = 'Constructor' } Fn.prototype = Object.create(Array.prototype) let a = new Fn() console.log(a instanceof Array) // true
Instanceof is used to detect whether the prototype attribute of a constructor appears on the prototype chain of an instance object, for example, a instanceof B, which means whether a is an instance of B, that is, whether B exists on the prototype chain of a
Conclusion:
- instanceof can accurately determine the reference (object) type, but cannot accurately detect the original type
- This method of detection is not secure because the orientation of the prototype can be modified
Object.prototype.toString.call()
Object.prototype.toString({}) // '[object Object]' Object.prototype.toString.call({}) // '[object object]' Object.prototype.toString.call('a') // '[object String]' Object.prototype.toString.call(1) // '[object Number ]' Object.prototype.toString.call(true) // '[object Boolean]' Object.prototype.toString.call(null) // '[object Null]' Object.prototype.toString.call(undefined) // '[object Undefined]' Object.prototype.toString.call(Symbol('a')) // '[object Symbol]' Object.prototype.toString.call(11n) // '[object BigInt ]' Object.prototype.toString.call(/a/) // '[object RegExp ]' Object.prototype.toString.call(new Date()) // '[object Date]' Object.prototype.toString.call([0, 1, 2]) // '[object Array]' Object.prototype.toString.call(function () { }) // '[object Function]' Object.prototype.toString.call(new Error()) // '[object Error]' Object.prototype.toString.call(new Set()) // '[object Set]' Object.prototype.toString.call(new Map()) // '[object Map]'
Each object has a toString() method that is automatically invoked when the object is represented as a text value or when an object is referenced as an expected string
By default, the toString() method is inherited by each Object object
If this method is not overridden in a custom object, toString() returns'[object type]', where type is the type of object