Two ways to convert other types to number
- Number
- parseInt/parseFloat
When are other data types converted to Number
- ParseInt /parseFloat(val))
- Hermit conversion (internal browser default to convert to Number before calculation)
- isNaN(val)
- Math operation (special case, + is not math operation in case of string, but string concatenation)
- When == is compared, some values need to be converted to numbers for comparison
Number([val])
console.log(Number('')) //0 console.log(Number('10')) // 10 console.log(Number('10px')) // NaN console.log(Number(true)) // 1 console.log(Number(false)) // 0 console.log(Number(null)) // 0 console.log(Number(undefined)) // NaN Console. log(Number(Symbol(''))) // Conversion console.log(Number(BigInt('10n'))) // 10 console.log(Number({})) // NaN console.log(Number([])) // 0 console.log(Number([10])) // 10 console.log(Number([10, 20])) // NaNCopy the code
ParseInt conversion rules
ParseInt ('10px12') // 10 parseInt('10.5px') // 10 parseFloat('10.5px') // 10.5Copy the code
5. Other data types are converted to strings
- toString()
- String()
- Hermit conversion
In addition, if there’s a string on either side of the plus sign, it’s not math, it’s a string concatenation to convert an object to a number, and you need toString.
6. Convert to Boolean
- !
- Boolean([val])
- Implicit conversion
Only 0, ‘ ’empty string, NaN, null, and undefined will be false in the + sign operation loop or in the conditional judgment, the others will be true
7. Exercises
1. The plus sign in JS is not necessarily a mathematical operation, as long as strings, objects, arrays appear on one side of the string will be string concatenation
let result = 10 + false + undefined + [] + 'Tencent' + null + true + {} console.log(result) // NaNTencentnulltrue[object Object]Copy the code
2. parseInt
ParseInt left to right, array found, NaN not found, isNaN will use Number to do the hermit conversion
parseInt('') // NaN Number('') // 0 isNaN('') // false =>isNaN(Number('')) parseInt(null) // NaN => parseInt('null') Number(null) // 0 isNaN(null) // false parseInt('12px') // 12 Number('12px') // NaN isNaN('12px') // true ParseFloat ('1.6px') + parseInt('1.2px') + Typeof parseInt(null) //2.6number isNaN(number (!! Number(parseInt('0.8'))) // false Typeof! parseInt(null) + ! isNaN(null) // 'booleantrue'Copy the code
3. == Related type conversion
{} == [] // false compares the address of heap memory [] == [] // false NaN == NaN // falseCopy the code
Different types of comparison
- Null == undefined // true, but === false (because the type is different), and the remaining null/undefined is not the same as any other data type
- The string == object is converted to a string
- If the data types on both sides of == are inconsistent, the rest need to be converted to numbers for comparison
Console. log(1==true) // false Converts different types to numbers console.log([] == false) // true console.log(! [] == false) //trueCopy the code
4 parseint interview questions
If parseInt([string], [radix]) is omitted or 0, then the radix defaults to 10 (special: string starts with 0x, radix defaults to 16). The radix is not between 2 and 36, and the final result is NaN
Let arr = [10.8, 0, 10, 25, 23] arr = map(parseInt) console.log(arr) // [10, NaN, 2,2, 11)Copy the code
Resolution: ParseInt (‘0’, 1) => parseInt(‘0’, 1) => parseInt(‘0′, 1) => NaN parseInt(’10’, 2) => 2 parseInt(’25’, 3) => only 2, 2*3^0 => 2 parseInt(’23’, 4) => 11