Core knowledge

  1. Number Indicates the Number data type
  2. IsNaN (detecting if a value is a non-significant number) method
  3. Autoincrement and its particularity
  4. Converts values of other types to numeric types
  5. Examples of array objects and map method callbacks

Number Number type

  1. Conventional digital
  2. NaN(NOT A NUMBER): Is NOT A NUMBER, but belongs to A NUMBER type
  3. Infinity value

NaN and isNaN (detecting if a value is an invalid number) methods

About NaN

NaN is not equal to any value, even itself;

NaN= =NaN     //false
NaN= = =NaN    //false
NaN! = =NaN    //true  
Copy the code

Object. Is (NaN,NaN) = true

IsNaN: Checks whether a value is a non-significant number

  • Returns true if it is not a significant number
  • Return false otherwise

When isNaN is used for detection, its mechanism will first verify that the value being tested is numeric, and if not, the value will be converted to numeric based on the Number() method before detection.

  • Such as isNaN (‘ 10 ‘); // is false because the transition is automatic before detection due to this mechanism

  • Anything that can be converted to a significant number is a significant number

           //isNaN([val])
           console.log(isNaN(10));//false
           console.log(isNaN('AA'));//True
           /* Actually happened before the test: Number('AA'); => isNaN(NaN); =>TRUE */
     		//
           console.log(isNaN('10'));//false
           /* The type conversion mechanism actually happens before detection: Number('10'); =>10 isNaN(10); =>false */
    Copy the code
isNaN('123') //false
isNaN(123) // false 
isNaN("") //false 
isNaN("1.2.3") // true 
isNaN("23px") // true
Number('"23px") = NaN 
isNaN(Number('aaa')) / / true Number ('aaa'") = NaN isNaN(Number(true)) // Enter 1 = false isNaN(Number(false)) // Enter 0 = false isNaN(Number(null)) // Enter 0 = false IsNaN (Number(undefined)) // Enter NaN = trueCopy the code

Increasing particularity

Due to the special properties of the + sign [can stand for mathematical operations], [can also stand for string concatenation]

So in i++; i+=n; i=i+n; There are also slight differences between the three forms of operations.

i++; Not quite like the other two, it represents pure mathematics

 let i = '10';
 i=i+1;//=>'10'+1=>'101'
 i+=1;//'10'+1=>'101'
 i++;//=>10=>10+1=>11
Copy the code

Converts values of other types to numeric types

There are two types of methods: manual conversion and implicit conversion

Implicit conversion: [default browser internal conversion to Number before calculation]

  1. isNaN([val])
  2. == mathematical operation (special case: + is not a mathematical operation in the case of a string or object, but a string concatenation)
  3. In some cases, the data type is converted to a number when == is compared

Manual switch

Number([val]) : converts all valid numeric characters(Commonly used in browser implicit conversions)

+ rules:

1 converts a string to a number: the empty string becomes 0 (except for the first dot, which exists as a decimal point), and if any non-valid numeric character occurs, the result is NaN
console.log(Number('12.5'));/ / 12.5
console.log(Number('12.5 px.));//NaN 
console.log(Number('12.5.5'));//NaN 
console.log(Number(' '));/ / 0
Copy the code
2 Converts booleans to numbers: true->1 false->0
console.log(Number(true));/ / 1
console.log(Number(false));/ / 0
/ / practice
console.log(isNaN(false)); //Number(false) =>0(valid digits) =>false
console.log(isNaN(true));  //Number(true) =>1 =>false
Copy the code
3 null->0 undefined->NaN

Null is converted to 0

Undefined is used to convert NaN

Undefined means undefined, undefined means no assignment, no assignment means “no value”, “no value” is converted to the value of Number is NaN.

console.log(Number(null)); //=>0 console.log(Number(undefined)); //=>NaN
console.log(isNaN(null)); //Number(null) =>0(valid Number) =>false
console.log(isNaN(undefined)); //Number(undefined) =>NaN =>true
Copy the code
TypeError: Cannot convert a Symbol value to a number. TypeError: Cannot convert a Symbol value to a number
console.log(Number(Symbol("123"))) 
//=>Uncaught TypeError: Cannot convert a Symbol value to a number
Copy the code
5 BigInt remove “n” (if the number exceeds the safe number, it will be treated as a scientific counting method)
console.log(Number(231n)) / / = > 231
Copy the code
6 Convert objects to numbers:

Regular object, regular expression object, date object…. All conversions to numbers are nans

Only array objects can be converted to numbers.

An array with only one value can be converted to a number

  • 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

Number({name:"xxx".age:5}) //=>NaN
1.= > ({name:"xxx".age:5}) [Symbol.toPrimitive] //=>undefined
2.= > ({name:"xxx".age:5}).valueOf() //=>{name: "xxx", age: 5}
3.= > ({name:"xxx".age:5}).toString() //=>"[object Object]"
4.= >Number("[object Object]") //=>NaN
Copy the code
console.log(Number([]));/ / = > 0
Number([10]) / / = > 10
1.= > [10] [Symbol.toPrimitive] //=>undefined
2.= > [10].valueOf() / / = > [10]
3.= > [10].toString() / / = > "10"
4.= >Number("10") / / = > 10
Copy the code
Number([10.20.30]) //=>NaN
Copy the code
7 Other special conversion cases
let time = new Date(a)Number(time) / / = > 1625126051393
time[Symbol.toPrimitive] ƒ [Symbol.toprimitive]() {[native code]} because time has this method and has three parameters number/string/default, the default argument will be passed
time[Symbol.toPrimitive]('number') / / = > 1625126051393
dir(time)
VM1101:1 Thu Jul 01 2021 15:54:11 GMT+0800(China Standard Time)Copy the code
Number(new Number(10)) / / = > 10
new Number(10) //=> Number {10} Outputs an object containing valueof()
1.= >new Number(10) [Symbol.toPrimitive] //=>undefined
2.= >new Number(10).valueOf() //=>10 Do not go down after it is found
Copy the code

Use scenarios for parseInt([val],[base])/parseFloat([val])

  • Note: parseFloat does not support the second argument!!

  • ParseInt can recognize a variety of int formats, such as base 8, base 10, and base 16, so parseInt can accept a second argument indicating how many bases to convert to.

    Rule: the value [val] must be a string, if not, firsttoString()Convert to a string; Then start with the first character on the left of the string and convert the found valid numeric character to the number “NaN”. 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

parseInt("10") / / = > 10
parseInt("010") / / = > 10
parseInt("10px") / / = > 10
Number('10px') //=> NaN
parseInt("10PX10") / / = > 10
parseInt("10.55 px") / / = > 10
parseInt(null) //=> NaN
parseInt(undefined) //=> NaN
parseInt(false);//=>NaN
parseInt(true);If the value is Boolean, null, or undefined, NaN will be returned
parseInt({}) //=>NaN 
parseInt([]) //=>NaN
 /* parseInt/parseFloat Will return a NaN ({}). The toString () = > "[object object]" parseInt/parseFloat (" [object object] ") = > NaN -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [] the toString () = > "/ /" empty string parseInt/parseFloat (" ") = > NaN * /
----------------------------
 console.log(parseInt([12]));/ / = > 12
 console.log(parseFloat([12.5]));/ / = > 12.5
 console.log(parseInt([12.13]));/ / = > 12
Copy the code

ParseInt Second parameter explanation — classic interview question

 let arr = [27.2.0.'0013'.'14px'.123];
 arr = arr.map(parseInt);
//array.map(function(currentValue,index,arr){}) {//array.map(function(currentValue,index,arr){}
 console.log(arr) /! *parseInt(27.2.0)
    parseInt('27.2'.10) - >'27'- >'27'As a10Base, convert to10Hexadecimal = >27
  parseInt(0.1) = >NaN-> System is not in the value range, =>NaN
  parseInt('0013'.2)
    parseInt('0013'.2) - >'001'- >'001'As a2Into the system (2The number within)10Hexadecimal = >1
      0*2^2 + 0*2^1 + 1*2^0

  parseInt('14px'.3)
    parseInt('14px'.3) - >'1'- >'1'As a3Into the system (3Within a few4Out of ternary range), convert to10Hexadecimal = >1
      1*3^0   

  parseInt(123.4)
    parseInt('123'.4) - >'123'- >'123'As a4Into the system (4The number within), converted to10Hexadecimal = >27
      1*4^2 + 2*4^1 + 3*4^0

  parseIntThe second value passed is a radix radix radix radix don't write or write0The default is10Base "If the first string passed begins with" 0x ", the default is16Radix "+ radix2~36Not in this range, the result of processing isNaN+ in the passed string, from left to right, find accord with radix hexadecimal values {met does not conform to the end to find}, to find the value of the as radix into the system, finally converted to10Base + converts the value of another base to10Base: "Sum by weight" *! /Copy the code