How do I work around JavaScript's parseInt octal behavior?

后端 未结 10 1793
Happy的楠姐
Happy的楠姐 2020-11-21 07:36

Try executing the following in JavaScript:

parseInt(\'01\'); //equals 1
parseInt(\'02\'); //equals 2
parseInt(\'03\'); //equals 3
parseInt(\'04\'); //equals          


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:56

    From the parseInt documentation, use the optional radix argument to specify base-10:

    parseInt('08', 10); //equals 8
    parseInt('09', 10); //equals 9
    

    This strikes me as pedantic, confusing, and verbose (really, an extra argument in every single parseInt?) so I'm hoping there is a Better Way.

提交回复
热议问题