Knowledge about

Quoting snail brother’s masterpiece juejin.cn/user/421298…

JavaScript determines data types in four ways

  1. typeof
  2. instanceof
  3. constructor
  4. Object.prototype.toString

typeof

The typeof operator returns a string representing the typeof the operation value

The syntax for determining data types using Typeof is Typeof Target. The following is an example:

// 'number' console.log(typeof 123) // 'string' console.log(typeof '123') // 'boolean' console.log(typeof true) // 'symbol' console.log(typeof Symbol(123)) // 'object' console.log(typeof []) // 'object' console.log(typeof {}) // 'function' console.log(typeof function(){}) // 'undefined' console.log(typeof undefined) // 'object' console.log(typeof null) // 'object' console.log(typeof new Date()) // 'object' console.log(typeof /\d/g) // 'object' console.log(typeof New Error()) typeof 'can accurately determine all basic data types except' null 'and' Function 'returns' object for' null 'and other reference data typesCopy the code

instanceof

The instanceof operator is used to check whether the constructor’s prototype property appears on the prototype chain of an instance object

The syntax for determining data types using instanceof constructor is target instanceof constructor. The following is an example:

// false console.log(123 instanceof Number) // false console.log('123' instanceof String) // false console.log(true instanceof Boolean) // false console.log(Symbol(123) instanceof Symbol) // true console.log([] instanceof Array) // true  console.log({} instanceof Object) // true console.log(function(){} instanceof Function) // TypeError: Right-hand side of 'instanceof' is not an object console.log(undefined instanceof undefined) // TypeError: Right-hand side of 'instanceof' is not an object console.log(null instanceof null) // true console.log(new Date() instanceof Date) // true console.log(/\d/g instanceof RegExp) // true console.log(new Error() instanceof Error)Copy the code

Determines whether the operation value is an instance of the specified constructor.

Disadvantages:

  1. Primitive data types cannot be determined because primitive data types are not instances of constructors, and there is no stereotype chain.
  2. Because the end of the prototype chain isObject.protype => null, so all stereotypes that reference data types exist on the chainObject.protype, so reference the data typeinstanceof ObjectReturn all the timetrue.
  3. The prototype chain can be modified, so the resulting value may not be accurate.

constructor

The syntax for using constructor to determine data types is target.constructor === constructor. The following is an example:

// Number
console.log((123).constructor)
// String
console.log('123'.constructor)
// Boolean
console.log(true.constructor)
// Symbol
console.log(Symbol(123).constructor)
// Array
console.log([].constructor)
// Object
console.log({}.constructor)
// Function
console.log(function(){}.constructor)
// TypeError: Cannot read properties of undefined (reading 'constructor')
console.log(undefined.constructor)
// TypeError: Cannot read properties of null (reading 'constructor')
console.log(null.constructor)
// Date
console.log(new Date().constructor)
// RegExp
console.log(/\d/g.constructor)
// Error
console.log(new Error().constructor)
Copy the code

All data types except null and undefined can be used to determine whether the operation value is an instance of the specified constructor. Null and undefined cannot be used because they are the basic data types that existed when the JavaScript runtime environment was created. No constructor attribute exists

Why do primitive data types have a constructor attribute? This is because JavaScript automatically converts the value of the base datatype to a wrapper object instance when the constructor attribute is obtained by the base datatype and destroys the instance immediately after use.

Disadvantages: The constructor attribute can be modified, so the resulting value may not be accurate.

Object.prototype.toString

Returns the type string of the object.

Use Object. The prototype. ToString judgment syntax is Object data type. The prototype. ToString. Call (target). The following is an example:

// '[object Number]'
console.log(Object.prototype.toString.call(123))
// '[object String]'
console.log(Object.prototype.toString.call('123'))
// '[object Boolean]'
console.log(Object.prototype.toString.call(true))
// '[object Symbol]'
console.log(Object.prototype.toString.call(Symbol(123)))
// '[object Array]'
console.log(Object.prototype.toString.call([]))
// '[object Object]'
console.log(Object.prototype.toString.call({}))
// '[object Function]'
console.log(Object.prototype.toString.call(function(){}))
// '[object Undefined]'
console.log(Object.prototype.toString.call(undefined))
// '[object Null]'
console.log(Object.prototype.toString.call(null))
// '[object Date]'
console.log(Object.prototype.toString.call(new Date()))
// '[object RegExp]'
console.log(Object.prototype.toString.call(/\d/g))
// '[object Error]'
console.log(Object.prototype.toString.call(new Error()))
Copy the code

All data types can be accurately judged.

conclusion

typeof instanceof constructor Object.prototype.toString
number
string
boolean
symbol
[]
{}
function() {}
undefined
null
new Date()
/\d/g
new Error()

The interview guide

  • Basically is the reason that remembers distinction and cannot judge clearly.

review

  • There is no