JavaScript - Preventing octal conversion

前端 未结 3 807
天命终不由人
天命终不由人 2020-12-12 01:21

I\'m taking a numerical input as an argument and was just trying to account for leading zeroes. But it seems javascript converts the number into octal before I can do anythi

3条回答
  •  天涯浪人
    2020-12-12 02:04

    If you type numbers by hand to script then not use leading zeros (which implicity treat number as octal if it is valid octal - if not then treat it as decimal). If you have number as string then just use + operator to cast to (decimal) number.

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

    The strict mode will not allow to use zero prefix

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

提交回复
热议问题