Why is parseInt(021, 8) === 15?

This is because 0n is octal notation in javascript, just like Oxn is hex notation:

console.log(021 === 0x11 && 021 === 17);

So what you wrote got evaluated as parseInt(17, 8);

Then this 17 number gets coerced to the string "17" and the result is 15.

console.log(parseInt(17, 8))

Note that all this would not have happened in strict mode, where 0n notation has been deprecated:

(function(){
  "use strict";
  // Syntax Error
  console.log(parseInt(015, 8))
})();


The first argument of parseInt should be a string. The same MDN link says:

The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation).

You can see that this works as expected:

console.log(parseInt("021", 8))

The problem in your tests is that you're using a number, not a string. When you use 021 as a number, as you already knows (when you said "numericals that begin with 0 are treated as an octal"), it gets converted to "17":

console.log(021.toString())

And that gives you the result you're seeing:

console.log(parseInt("17", 8))


When you type 021 then this is valid octal number and because prefix 0 JS convert it to decimal 17. But if you type not valid octal number e.g. 019 then JS will NOT convert it but treat as decimal

console.log(021)    // octal
console.log(019)    // not octal

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

Tags:

Javascript