4. Data type interpretation of number, Symobol, BigInt

4.1 Number Type Number

  • Integer, floating point (decimal), positive, negative, zero
  • NaN: not a number is not a significant number, but it is of type number
    • typeof NaN => 'number'
    • Scenario: Often occurs when you convert other types to numbers, and if you cannot convert to a significant number, the result is NaN
    • NaN ! NaN is not equal to any value, including itself
    • IsNaN ([val]): checks if val is “not a significant number”, returns true if it is not, and false if it is. If val is not numeric, the browser first implicitly converts it to number and then checks.
    • Object. Is (NaN, NaN) -> true The method provided in ES6 is not compatible with Internet Explorer
    • //let n = ?
      //if (n === NaN) {
      // The condition never holds
      // console.log('n is not a significant number ')
      / /}
      letn = ? ;if(isNaN(n)){
        console.log('n 'is not a significant number.)}Copy the code
  • Infinity is the value of Infinity

4.2 Bigint

  • Bigint tarsus
    • Maximum/minimum security number in JS:MAX_SAFE_INTEGER/number.min_safe_integer/math.pow (2,53) -1
    • 16, 9007199254740991-9007199254740991
    • Numerical calculations of values beyond this range are inaccurate
    • The ID stored on the server side of the scenario is a very large value (we can process the database based on longInt). Now we return this value to the client. After the client obtains the value, it needs to process the calculation.
    Number.MAX_SAFE_INTEGER
    9007199254740991
    9007199254740991 + 10
    9007199254741000
    9007199254740991n + 10n
    9007199254741001n
    typeof 9007199254741001n
    "bigint"
    Bigint("90071992547409919007199254740991") - >90071992547409919007199254740991n
    // A number followed by "n" is BigInt
    // The long result of the calculation is converted to a string based on toString, which is passed to the server
    Copy the code

4.3 Symbol

  • Symbol Unique value type
    • Symbol() // creates a unique value Symnol(' XXX ') // creates a unique value, but sets the mark
    • New Symbol() Uncaught TypeError: Symbol is not a constructor // Not allowed to be executed by new
    • Purpose 1: Assign a unique attribute to an object. Object members must be string & symbol values. Map allows attribute names of different types (including objects).
    • Purpose 2: It is an implementation of many of the built-in mechanisms
      • Symbol.toPrimitive
      • Symbol.hasInstance
      • Symbol.toStringTag
      • .
    • console.log(Symbol('AA') = = =Symbol('AA')); // False creates two unique values
      let sy = Symbol('AA')
          ys = sy
      sonsole.log(ys === sy) //true creates only a unique value
      let key = Symbol('KEY')
      let obj = {
          // Attribute name: 'name' 'age' '0' '1' Symbol() Symbol('KEY') '[object object]'
          name:'zs'.age:23.0:100.1:220[Symbol()] :300,
          [key]:400.// Convert the object to a string to act as its property name
          [{
            name:'xxxx'}] :500
      }
      console.log(obj[Symbol()]) //undefined uses the newly created unique value
      console.log(obj[key]);/ / 400
      console.log(obj[0]);//100 -> obj['0']
      Copy the code

5. Data type conversion

5.1 Converting other data types to Number

  • Number([val])
    • It is commonly used in implicit conversions in browsers
    • Rules:
    1. Converts strings to numbers: The empty string becomes 0, and if any non-valid numeric character occurs, the result is NaN
    2. Converts a Boolean value to a number true -> 1 false -> 0
    3. null -> 0 undefined -> NaN
    4. TypeError: Cannot convert a Symbol value to a number. TypeError: Cannot convert a Symbol value to a number
    5. Bigint removes n (if the number exceeds the safe number, it will be treated by scientific notation)
    6. Converts objects to numbers
    • Call the Symbol. ToPrimitive method of the object first, if it does not exist
    • Call valueOf of the object to get the original value, if not the original value
    • Call the object’s toString to make it a string
    • Finally, the string is converted to a Number based on the Number method
  • parseInt([val],[radix])
  • parseFloat([val])
    • Generally used for manual conversion
    • Rule: the value of [val] must be a string, if not converted to a string; Start with the first character to the left of the string, and convert any valid numeric character found to a number. If an invalid numeric character is encountered, the search is stopped, regardless of whether there are other valid numeric characters. ParseFloat can recognize one more decimal point
let arr = [27.2.0.'0013'.'14px'.123]
arr = arr.map(parseInt)
/ * parseInt (27.2, 0) / / 27.2 - > '272.2' - > '27' - > '27' as decimal, ParseInt (0,1) => NaN parseInt('0013', 2) // '0013' -> '001' -> use '001' as base 2, Is converted to a decimal = > 1 * 2 * 2 ^ ^ 2 + 0 0 1 + 1 * 2 ^ 0 = 1 parseInt (' 14 px, 3) / / '14 px' - > '1' - > '1' as in base 3, 1 * 3^0 parseInt('123', 4) // '123' -> >27 1*4^2 2*4^1 3*4^0 parseInt passes the second value which is a radix radix + radix does not write or write 0, Defaults to base 10 (if the first string passed starts with '0x' then defaults to base 16) + radix value range: 2~36, outside this range, processing results are NaN + in the passed string. From left to right, find the values that accord with the radix radix system (stop the search if it doesn't), treat the found values as radix radix system, and finally convert them to base 10 + convert the values of other bases to base 10: 'sum by weight' */
console.log(arr) // [27, NaN, 1, 1, 27]
Copy the code

5.2 Convert other types to Booleans

  • All values are true except 0, NaN, empty string, null, and undefined

5.3 Converting other types to Strings

  • [val].toString() & String([val])
    • Raw value type: Enclosed in quotes, bigint removes n
    • Object type value:
      • Call Symbol. ToPrimitive
      • If it does not, call valueOf to get the original value, and if it does, it is converted to a string
      • If it is not a raw value, toString is called to convert it to a string
      • Special: common object into a string is’ [object object] ‘- > object. The prototype. ToString
  • String concatenation for “+”
    • If you have two sides, one side is a string, it becomes a string concatenation

    • There are two sides, one side is the object, according to Symbol. ToPrimitive -> valueOf ->toString processing, becomes a string, directly according to the string concatenation processing

      • Special circumstances:{} + 10-> 10 {} is considered a block of code and handles only the +10 operation
    • + only appears on the left side eg: +[val] This converts val to a number ++ I (operation before sum) & I ++(operation before sum)

    • let result = 100 + true + 21.2 + null + undefined + 'Tencent'+ + []null + 9 + false;
      // 100 + true + 21.2 + null 122.2
      // undefined NaN
      // NaN + 'Tencent' 'NaNTencent'
      // 'NaNTencent' + [] + null + 9 + false 'NaNTencentnull9false'
      console.log(result) //'NaNTencentnull9false'
      Copy the code

5.4 rules for converting ‘==’ when comparing

  • ‘==’ is equal, the two data types are different, need to convert to the same type, and then compare
    • Object == string object converts toString symobol. toPrimitive -> valueOf -> toString
    • Null == undefined -> true Null /undefined is not equal to any other value

    null === undefined -> false

    • Object == Objects compare heap memory addresses, which are equal
    • NaN ! == NaN
    • Other than that, if the two types don’t match, the rest is converted to a number and then compared
  • ‘===’ is absolutely equal, if the two sides have different types, then it is false, and does not cast the data type
  • On the topic
console.log([] == false); //true
//[] -> Symbol.toPrimitive()/undefined valueOf()/[] toString/'' -> 0
// false -> 0 0 == 0 true
var a = ?
if (a == 1 && a == 2 && a == 3) {console.log('ok')}/* '=='; The mechanism for converting data types, you need to convert a to a number because converting an object to a number, Obj [Symbol. ToPrimitive] = function(hint) {// hint: Number } var a = {I :0, ToString [symbol.toprimitive](){return ++this. I}} --------- var a = [1, 2, 3] a. range (range, range, range, range, range, range) Object.defineproperty (window, 'a', {get(){return ++ I}}) */
Copy the code

If you think it’s ok, please give XDM a thumbs up and click on the column below to see other articles at !!!!