Javascript, why treated as octal

前端 未结 6 521
我在风中等你
我在风中等你 2020-12-06 20:58

I\'m passing as parameter an id to a javascript function, because it comes from UI, it\'s left zero padded. but it seems to have (maybe) \"strange\" behaviour?



        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 21:31

    If number came from server as zero padded string then use +"0000022115"

    console.log(+"0000022115")
    
    if (021 < 019) console.log('Paradox');

    JS treat zero padded numbers like octal only if they are valid octal - if not then it treat it as decimal. To not allow paradox 'use strict' mode

    'use strict'
    
    if (021 < 019) console.log('Paradox');

提交回复
热议问题