js parseInt
时间: 2023-11-24 15:05:34 浏览: 80
js中parseInt函数浅谈
The parseInt() function in JavaScript parses a string and returns an integer. It takes two arguments: the first is the string to be parsed, and the second is the radix or base of the number system to be used for parsing. If the second argument is not provided, the function assumes a radix of 10.
For example, the following code will parse the string "10" and return the integer 10:
```
const num = parseInt("10");
console.log(num); // output: 10
```
If the string to be parsed is not a valid number, the function will return NaN (Not a Number):
```
const num = parseInt("hello");
console.log(num); // output: NaN
```
If the radix is specified, the function will parse the string using the specified base. For example, to parse the binary number "1010" as an integer, the radix argument should be set to 2:
```
const num = parseInt("1010", 2);
console.log(num); // output: 10
```
阅读全文