Home > Article > Web Front-end > Why Does JavaScript Treat Numbers With Leading Zeroes as Octal?
Leading Zeroes in JavaScript Number Literals: Understanding Octal Conversion
When JavaScript encounters a number literal with a leading zero, it interprets it as an octal (base-8) number due to historical reasons. Leading zeroes were once allowed as an extension in ECMAScript 3, enabling developers to specify octal numbers in their code. However, this practice was not standardized and led to confusion, especially in strict mode.
In ECMAScript 5, using leading zeroes to represent octal numbers was fully deprecated. To avoid potential errors, developers are advised to remove leading zeroes from numeric literals or explicitly specify the base using the following prefixes:
If you encounter an integer literal with a leading zero in your code, it's important to understand how it will be interpreted by the JavaScript engine. If it's meant to be a decimal number, ensure that the leading zero is removed.
To prevent JavaScript from interpreting a number literal as an octal number, use one of the following techniques:
The above is the detailed content of Why Does JavaScript Treat Numbers With Leading Zeroes as Octal?. For more information, please follow other related articles on the PHP Chinese website!