Is parseInt() supposed to work like this?

前端 未结 7 1929
不思量自难忘°
不思量自难忘° 2020-12-17 23:15

If I write this script :

alert(parseInt(\"123blahblahblah456\"));

I get the alert with the value 123

Ideally, shouldn\'t th

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-17 23:35

    parseInt attempts to parse the string until it finds a non-integer value, at which point it returns whatever it had.

    So if the string is:

    • 1234abcd - it returns 1234
    • 1a3f - it returns 1
    • a14883 - it returns NaN
    • 1.5 - it returns 1
    • -1.3a - it returns -1

    Same with parseFloat except that won't break on a .

    • 1234abcd - it returns 1234
    • 1a3f - it returns 1
    • a14883 - it returns NaN
    • 1.5 - it returns 1.5
    • -1.3a - it returns -1.3

提交回复
热议问题