Explain the data types in JavaScript in detail and the methods of detecting data types

  • 2021-08-21 19:39:45
  • OfStack

1. What are the data types in 1. js?

In js, there are five basic data types, which are string, number, boolean, null and undefined. However, in ES6, a new basic data type Symbol (representing the value of one without two) is added, which is mainly set to prevent the conflict of attribute names fundamentally.

In addition to the basic data types, there are reference data types object, which are also called complex data types, including our common Array, Object, Function and so on.

So there are now seven data types in js.

PS: The Symbol data type is generated by the Symbol function. That is to say, the attribute name of an object can now have the original string and the current Symbol type. If the attribute name belongs to Symbol type, it is unique and has no 2, which can ensure that it will not conflict with other attribute names.

The Symbol function can also take a string argument representing a description of an Symbol instance.


let s = Symbol()
console.log(typeof s) // "symbol"

let s1 = Symbol('s1')
let s2 = Symbol('s2')
console.log(s1) // Symbol(s1)
console.log(s2) // Symbol(s2)

Note: The parameters of the Symbol function represent only a description of the current instance, so the return values of Symbol with the same parameters are not equal.

2. Methods of js data type detection (there are several kinds of data type detection):

1. typeof: typeof1 is generally used to detect basic data types, because it returns Objcet when detecting reference data types


console.log(typeof 1) // "number"
console.log(typeof 'a') // "string"
console.log(typeof undefined) // "undefined"
console.log(typeof true) // "boolean"
console.log(typeof null) // "object"
console.log(typeof ) // "symbol"
function fun(){ }
console.log(typeof fun) // "function"

Note: typeof detection of null also returns Object, which is the BUG left over from js1. Detecting function with typeof returns' function '.

2. instanceof This method is mainly used to prepare for detecting reference data types (not basic data types), and to detect whether the prototype attribute of the constructor appears anywhere in the object prototype chain.


let fun = function(){ }
fun instanceof Function  //true
let obj ={ }
obj instanceof Object //true
let arr = [ ]
arr instanceof Array //true

I was once asked a question by the interviewer. What did instanceof return? At that time, I said to return to true for my own reasons. Now think about Emmm …


1 instanceof Number //false 
null instanceof Object // false

The original value of a variable directly accessed by the instanceof operator does not automatically create a wrapper class. Therefore, it cannot be used to judge the basic data type.

3. Object. prototype. toString () can be used to prepare for detection of all data types.


Object.prototype.toString.call([])
// "object Array"
Object.prototype.toString.call(1)
// "object Number"
Object.prototype.toString.call(null)
// "object Null"
Object.prototype.toString.call(undefined)
// "object Undefined"
Object.prototype.toString.call({})
// "object Object"
Object.prototype.toString.call(function add(){})
// "object Function"
....

4. constructor returns a Boolean value by detecting the constructor pointing of the type in the prototype chain.


let arr =[]
arr.constructor==Array
// true
let fun = function(){}
fun.constructor==Function
//true

Note: null and undefined have no constructor attribute and can be judged by other methods.

Through several summaries, I have a deep understanding of the data type of js and how to detect the data type. Don't panic in the next interview ~

The above is a detailed explanation of the data types in JavaScript and the detailed content of the methods of detecting data types. Please pay attention to other related articles on this site for more information about JavaScript data types!


Related articles: