Do you know what the following results are? Feel free to give us your calculations in the comments section
parseInt(6.1 electronics.10)
parseInt('10'.10)
parseInt(10 '2'.10)
parseInt(NaN)
parseInt(NaN.36)
['1'.'7'.'11'].map(parseInt)
['1'.'2'.'3'].map(parseInt)
parseInt('0x1')
parseInt('0x11')
Copy the code
parseInt
parseInt(str[, radix:)// Return an integer or NaN
Copy the code
ParseInt converts a parameter STR to base 10 according to radix radix.
Specifying the radix explicitly will save you a lot of trouble, and you’d better know exactly what strings STR is.
str
The value to be parsed,It should be a stringIf it is not a string, run theToStringRule conversion,str
Leading and trailing Spaces are omitted;radix
Represents thestr
Integer between 2 and 36 (including 2 and 36). Pay special attention to this cardinalityradix
Don’t fill inOr for0Or forundefinedWhen,str
以0x
or0X
The beginning is defaultradix
为16
.str
以0
At the beginning,radix
May be8
或10
(this is not certain, depending on the context), if it starts with any other valueradix
为10
;- When the cardinality is between [2, 36], to
radix
Convert to cardinality;
- Return value: the leading and trailing Spaces are automatically omitted, and NaN is returned if the first character that is not a space cannot be represented
- Omit leading and trailing Spaces, for example
parseInt(' 3 10 ', 10)
str
If there are multiple Spaces, the leading and trailing Spaces will be omitted from3
At first, if any more whitespace is encountered, it cannot be parsed and returns directly3
; - if
str
If the first non-space character of cannot be converted to a numeric type, NaN is returned, for exampleparseInt('2', 2)
parseInt('a', 10)
So the first digit exceeds the number represented by the radix energy, so NaN is returned; - Stop conversion if an unconvertible value is encountered, as in
parseInt('10a11', 10)
str
The first two1
和0
, can be expressed in decimal,a
If not, the conversion stops and returns10
;
- Omit leading and trailing Spaces, for example
Conclusion:
str
Leading and trailing Spaces are omitted- if
parseInt
Met does not belongradix
A character in the cardinality specified by the argument (except the leading and trailing Spaces) is ignored, as is any character following it; - In the
parseInt
Make sure you know before you make the switchstr
Is a string,radix
Also do not omit, to avoid inference which leads to the departure from the original intention; radix
Explicitly specify belonging to [2, 36] (including boundaries)
The above rule may be a little tricky, but here are a couple of demos.
// [parseInt('1', 0), parseInt('2', 1), parseInt('3', 2)]
// Radix = 0; STR = 1; parseInt(1, 10) => 1
// [1, NaN, NaN]
['1'.'2'.'3'].map(parseInt) = > [1.NaN.NaN]
// Remove the leading and trailing Spaces
parseInt('10'.10) / / 10
If there is a space in the middle, return the preceding number
parseInt(10 '3'.10) / / 3
// Base 10 cannot represent a
parseInt(' a 10 '.10) // NaN
parseInt('0x1') // Radix 16
+'0x1' / / 1
Number('0x1') / / 1
// String with e on parseInt may trigger an incorrect result
parseInt('6.1 electronics'.10) / / 6
+'6.1 electronics' / / 6.1 e+23
Number('6.1 electronics') / / 6.1 e+23
parseInt(6.1 electronics.10) / / 6
+6.1 electronics / / 6.1 e+23
Number(6.1 electronics) / / 6.1 e+23
Copy the code
ParseInt Specifies the type conversion involved
STR should be a string you already know. What happens if you pass in numbers and other types? Let’s look at the rules for the ToString internal conversion.
You can see that undefined and NULL are converted to the corresponding string.
Convert digital
For numeric types, after article 5, it is very difficult to understand, including the conversion of the following numbers. A number can be represented by scientific notation, like S *10ew(s on the closed interval [1, 10]), and then w is represented by conventional notation if it is less than or equal to 20, and exponential notation if it is greater than 20, as shown in the figure below.
In simple terms, the digital transformation rules is application Number. The prototype. The toString () [radix]
Radix is [2, 36] value of closed interval, default is 10 if left blank, cannot be 0, parseInt is 10.
Converting objects
Converting an object to a string involves ToPrimitive’s internal processing mechanism, which will be covered in more detail in the next article. Here is an overview of the conversion process.
ParseInt (OBj, radix) converts the OBJ object to a string and then to the corresponding result according to the radix. Obj has an effect on the obJ result of valueOf, toString, Symbol. ToPrimitive.
var obj = {
value: 1,
valueOf() {
console.log('valueOf')
return 2
},
toString() {
console.log('toString')
return '3'},Symbol.toPrimitive](hint){
console.log(hint)
if(hint === 'string') {
return '4'
}
return 5}}parseInt(obj, 10)
// string
// 4
// Remove the Symbol. ToPrimitive attribute and execute
// toString
/ / 3
Copy the code
Because it is converted to a string and toString returns a raw value, valueOf is not executed.
That’s all for today’s article, thanks for reading ~
Feel free to comment in the comments below.
Welcome to pay attention to my nuggets and public number, focus on improving the core competitiveness of front-end programmers, constantly explore [front-end specifications], [framework principle], through the analysis of source code to tell you how to [participate in open source projects], enhance visibility, establish personal brand.