This is the 4th day of my participation in Gwen Challenge. For details, see: Gwen Challenge #js data type classification

  • Basic data types

    • number
    • string
    • boolean
    • null
    • undefined
    • symbol

    1.static.Symbol 2.Symbol.prototype

  • Reference data type

    • Object 1. Common object 2. Array object 3. Regular object 4. Date object 5.JSON object 6.Set 7.Map
  • Functions 1. ordinary functions 2. constructors 3. arrow functions 4. generator functions

The number type includes positive numbers, 0, negative numbers, decimals, and NaN(not a number).

typeof (NaN) // number
Copy the code

The next thing to notice is NaN! =NaN, so the following judgment will not take effect when we perform it.

let value = NaN; If (value==NaN){Copy the code

So how do we know if the value of a variable is the value of NaN? There are two ways:

 let value  =  NaN;
 isNaN(value)// true
Copy the code

The second method is through object.is ()

Object.is(NaN, NaN) // object.is(), which is used to determine whether two values are equal, or if two objects share the same storage spaceCopy the code

Since NaN is usually obtained by casting, there are several ways to show the conversion if you convert other data types to number

 let  a ='String'
// (1) Number()
Number(a)
//  (2) parseInt()/ parseFloat()
parseInt(a)
Copy the code

It should be noted that although the methods in this set can convert other data types to number types, their conversion rules and mechanisms are different. First, if the Number conversion is Boolean, true and false will be converted to 1 and 0, respectively. If the string is’ ‘, then it will be converted to 0; (2) If the string contains a space before it, the space will be removed for conversion

let  str  ='     11111';
Number(str)//1111
Copy the code

(3) Strings containing valid hexadecimal characters, such as ‘0xf’, are converted to phase-sized decimal notation

let str  ='0xf'
Number(str)// 15
Copy the code

(4) If there is a 0 before the string, the preceding 0 will be ignored and converted

let str  ="000033333"
 Number(str) //33333
Copy the code

(5) Numbers mixed with other non-numbers are converted to NaN

let str  ='1111 ddd'
 Number(str) // NaN
let str1  ='ddd1111 '
 Number(str1) // NaN
Copy the code

ParseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1) parseInt (1

parseInt('11111dddddd222222') //11111
Copy the code

(2) If the string contains a space before it, the space will be removed for conversion.

parseInt('     11111dddddd222222') //11111
Copy the code

(3) All other cases are converted to nans

 parseInt(true)// NaN;
parseInt('ddddd111111')//NaN
Copy the code

(4) Using parseInt can also convert binary to gold hexadecimal or any other prohibited string into an integer

Var num1 = parseInt (" AF ", 16); Var num2 = parseInt("AF"); //NaN var num3 = parseInt("10",2); Var num4 = parseInt("sdasdad"); //NaNCopy the code

ParseFloat () is similar to the parseInt conversion rule, except that it cannot be converted to the corresponding base and can continue parsing if it encounters a decimal point.

There are three types of implicit conversions: (1)+ (1+true) (2) == (3) isNaN() will be converted to a number for comparison

##String (1)””, (2) convert other types of value to String. Explicit conversion (1) toString() (2)String() Implicit conversion (1)+ plus In addition to math you can concatenate strings. Now let’s talk about the concatenation rule for the plus sign. (1) If a string appears on either side of the plus sign, then a concatenated string will appear.

 let n  = 10
let res  = n  + '10' // 1010
Copy the code

(2) On both sides of the plus sign, if one side is an object, it may also be a string concatenation _ object appears on the right side

 let m  =  10
 let res  =  m +  {} //  "10[object Object]"
Copy the code

The _ object is on the left

{} + 10 // 10 {} + 10 // 10 {} + 10 // 10 ({} + 10) // "[object object]10" // The browser will think that {} is involved in the operation, so the return is string concatenationCopy the code

The _ object is of type new Number, and the new Nnumber is converted to type Number for calculation

 let m  = 10, 
let obj  = new Number(10)
 obj+ m  // 20
Copy the code

The _ object contains properties of value

let test  = {
value: 10
}
test+ 10 // "[object Object]10"
Copy the code

Looking at the example above, what is the underlying mechanism if one side of the plus sign is object computation? Tests whether the object’s symbol.toprimitive property exists, if so, based on that value; if not, tests the object’s valueOf() based on that value if it is of primitive type; if it is not of primitive type, based on that value. Get the object’s toString() and concatenate it. For example, let’s write an object with the Symbol. Primitive property:

let obj  ={
[Symbol.toPrimitive]: function (){
return 10 
}
}
obj+ 10 // 20
Copy the code

Then our new Number(10) + 10 is 20 because new Number(10).valueof ()//10. Anything else that gets converted to a string is through toString()

(3) If there is only one side after the plus sign, it will be converted to a number if it is a string

let m  = '10'
console.log(+m) // 10
console.log(++m) // 11
Copy the code

The ##Symbol type is used to create unique values. (1) Set a Symbol attribute to the object, which is a unique attribute to reduce the conflict on the attribute. For example, to create a unique property for an object we can use

let obj  = {
[Symbol()]: '11111'

}
obj[Symbol] // undefined
Copy the code

But we can’t take out the value of this unique property, so we can define a variable called x

let x  =  Symbol()
let obj  = {
[x]: 1
}
 obj[x] // 1
Copy the code

(2) to macro manage some unique identifier, also used unique values. For example, Symbol. ToPrimiteve above. Symbol. HasInstance, Symbol. ToStringTag (), Symbol. Iterator Symbol is a constructor, but we cannot create it using new Symbol(), and the two symbols are not the same

Symbol('a') ===Symbol('a') // false
Copy the code

MAX_SAFE_INTEGER or number. MIN_SAFE_INTEGER overflow causes calculation errors.

Let a = number. MAX_SAFE_INTEGER // 9007199254740991 a+ 10 // 9007199254741000 We can do that with large numbersCopy the code

BigInt(10) BigInt(10) BigInt(10) BigInt(10

 let  b  = BigInt(9007199254740991)
let d  = 10n
b +d //9007199254741001n
Copy the code