javascript – parseInt (null, 24) === 23. Why?


How did the converted NaN and 24 become 23?

for(var i = 1; i <= 36; i++)
  console.log(i, parseInt(null, i), parseInt("null", i));


A simple coincidence.

parseInt(null, 24)

means to cast the string "null" (a string because the first parameter passed to parseInt() always converted to a string) to a base 24 number.

"n" in this case is 23 (because there are 9 digits (other than zero) and "n" is 14 letters of the alphabet. 9 + 14 = 23).

"u" – 30. Already goes beyond the radix 24. Therefore, 23 returned, that is, the number received up to this moment.

In other words, parseInt(null, 24) === parseInt("null", 24) === parseInt("n", 24) === 23 .

Hence the answer – 23 === 23true .

parseInt(null, m) === 23 will always return true if m is between 24 and 30.

This particular case is interesting only because null is passed to the first parameter as a value, and not as a string. This confused you. And null is, perhaps, the only meaning in the language, when converted to a number with a base 24 of which the result is as close as possible to the very base of the number system.

How did the converted NaN and 24 become 23?

And about this part. This is not NaN and 24 "transformed", but only null , which, when converted to a number (with base 24+) by means of the parseInt method, gives 23.

That is, the parseInt method takes as the first argument what needs to be converted, and as the second what will be the base of the number system .

More about the method itself here , about the "number systems" here (more on the corresponding query in Google; look also about the " base of the number system"), other answers to this question (in English, though) are here .

Scroll to Top