Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)

前端 未结 4 526
攒了一身酷
攒了一身酷 2020-12-03 17:23

Why are Octal numeric literals not allowed in JavaScript strict mode? What is the harm?

4条回答
  •  广开言路
    2020-12-03 17:48

    Why is Octal numeric literals not allowed in javascript strict mode? What is the harm?

    Octals in JS have historically been a non-standard extension to the standard (in ES5, which introduces strict mode, they're in Annex B, which is a collection of non-standard features that most implementations support: except it defines octals in a way incompatible with what websites require), and strict mode made an attempt to disallow all non-standard extensions. The "why" as to why they were never standardised is an obvious related question, and that I'm unaware of.

    In case a developer need to use Octals (which can mistakenly change a number's meaning), is there a work around?

    As @Amit answered, parseInt with its second argument as 8 still works in strict mode.

提交回复
热议问题