1.1 Utilizing functions
ParseInt Parses the string and returns an integer
ParseFloat parses the string and returns a floating point number, or NaN if the string does not begin with a number
console.log(parseInt('123')) / / 123
console.log(parseInt('123d')) / / 123
console.log(parseFloat('123.456')) / / 123.456
console.log(parseFloat('aa123.456')) //NaN
console.log(parseFloat('123.4 d56')) / / 123.4
Copy the code
1.2 Number
Some cast functions are provided in js
console.log(Number('123'>>0)) / / 123
console.log(Number('123d'>>0)) / / 0
console.log(Number(+'123')) / / 123
console.log(Number(+'123d')) //NaN
console.log(Number('123'-0)) / / 123
console.log(Number('123'*1)) / / 123
console.log(Number('123'/1)) / / 123
Copy the code
1.3 Weak type conversion
Bitwise non-(~~) operator (>>), plus operator (+), four operators (-, *, /)
console.log(Number('123')) / / 123
console.log(Number('123'>>0)) / / 123
console.log(Number(+'123')) / / 123
console.log(Number('123'-0)) / / 123
console.log(Number('123'*1)) / / 123
console.log(Number('123'/1)) / / 123
Copy the code
Feel free to like and comment if you find it helpful. The views expressed above are personal. Please correct any errors. If you are interested in the front, welcome to my personal blog sundestiny. Making. IO/myblog /