parseInt(null, 24) === 23… wait, what?

前端 未结 6 2020
栀梦
栀梦 2020-12-12 10:08

Alright, so I was messing around with parseInt to see how it handles values not yet initialized and I stumbled upon this gem. The below happens for any radix 24 or above.

6条回答
  •  旧巷少年郎
    2020-12-12 10:23

    parseInt uses alphanumeric representation, then in base-24 "n" is valid, but "u" is invalid character, then parseInt only parses the value "n"....

    parseInt("n",24) -> 23
    

    as an example, try with this:

    alert(parseInt("3x", 24))
    

    The result will be "3".

提交回复
热议问题