Javascript

parseIntnull 24 23 wait what

27 September 2026 · 10 min read

parseIntnull 24  23 wait what

Have you ever encountered a JavaScript quirk that left you scratching your head? One such enigma is the expression parseInt(null, 24) === 23. At first glance, this might seem like complete nonsense. Why would passing null with a radix of 24 result in the integer 23? This seemingly bizarre behavior stems from how JavaScript’s parseInt() function handles type coercion and radix values. Understanding this oddity illuminates some of the underlying mechanics of JavaScript, specifically how it handles null values and how different radix values influence the parsing process. By exploring this example, we can gain a deeper appreciation for the nuances of JavaScript and improve our ability to debug and write more robust code. Let’s dive into the details of this intriguing puzzle and unravel the mystery behind parseInt(null, 24) === 23.

Understanding parseInt() in JavaScript

The parseInt() function in JavaScript is used to parse a string and return an integer. It takes two arguments: the string to be parsed and an optional radix, which specifies the base of the numeral system to be used (e.g., 10 for decimal, 16 for hexadecimal). If the radix is omitted or is 0, JavaScript assumes the base 10 unless the string begins with “0x” or “0X”, in which case it assumes base 16. It’s crucial to understand that parseInt() attempts to convert the provided string into an integer, stopping at the first character it cannot interpret as a digit in the specified radix. According to the ECMAScript specification, the parseInt function first converts the input to a String. This initial type conversion is where our puzzle begins to unravel.

The behavior of parseInt() can be unpredictable if not used carefully. For instance, leading and trailing whitespace is ignored, but if the string starts with a non-numeric character (other than an optional sign), parseInt() will return NaN (Not-a-Number). Understanding these nuances is essential for writing correct and maintainable JavaScript code. Proper usage of parseInt() requires specifying the radix to avoid unexpected results, especially when dealing with user input or data from external sources. Failing to specify the radix can lead to subtle bugs that are difficult to track down. For example, parseInt("010") might be interpreted as 10 or 8 depending on the JavaScript engine and the absence of a specified radix.

Furthermore, parseInt() exhibits some interesting behaviors with different data types. When passed a number, it will typically convert that number to a string before parsing. For example, parseInt(123.45) is effectively the same as parseInt("123.45"), which returns 123. However, the real surprise comes when we pass null, which leads us back to our original puzzling expression. We can avoid these pitfalls by always providing an explicit radix. This ensures consistent behavior across different JavaScript environments. By explicitly setting the radix, you tell the function exactly how to interpret the string, eliminating any ambiguity.

The Role of Type Coercion: null to “null”

The key to understanding why parseInt(null, 24) === 23 lies in JavaScript’s type coercion. When null is passed to parseInt(), JavaScript first converts it to a string. In JavaScript, String(null) results in the string “null”. Therefore, the expression becomes effectively parseInt("null", 24). Now, the parseInt() function attempts to parse the string “null” using a radix of 24. This is where the magic happens. The function processes the string from left to right, looking for characters that are valid digits in base 24.

In base 24, the digits are 0-9 and a-n. The first character in “null” is “n”, which is a valid digit in base 24, representing the value 23. The next character is “u”, which is not a valid digit in base 24. Therefore, parseInt() stops parsing at “n” and returns the integer value represented by “n” in base 24, which is 23. This explains why parseInt(null, 24) evaluates to 23. According to a study on JavaScript type coercion, these implicit conversions can lead to unexpected results if not handled carefully. ECMAScript specification Section 9.1 details these type conversion rules.

To further illustrate this concept, consider another example: parseInt("abc", 16). Here, the radix is 16 (hexadecimal), and “a”, “b”, and “c” are valid hexadecimal digits representing 10, 11, and 12, respectively. Therefore, parseInt("abc", 16) returns 2748 (10 16^2 + 11 16^1 + 12 16^0). Similarly, if we were to use parseInt("10", 2), the result would be 2 because it’s parsing the binary “10”, which is 2 in decimal. Understanding how different radix values affect the interpretation of strings is critical for avoiding unexpected results when using parseInt(). The key takeaway is that JavaScript’s type coercion, coupled with the parsing logic of parseInt(), leads to this behavior. This highlights the importance of being aware of JavaScript’s type conversion rules and how they interact with built-in functions.

Radix and its Impact on Parsing

The radix parameter in parseInt() is crucial because it dictates the base in which the input string should be interpreted. If the radix is not specified, JavaScript attempts to infer it, which can lead to unpredictable behavior. When a radix is provided, parseInt() uses it to determine which characters are considered valid digits. For instance, in base 10 (decimal), only the characters “0” through “9” are valid digits. In base 16 (hexadecimal), the characters “0” through “9” and “a” through “f” are valid digits. The radix must be an integer between 2 and 36, inclusive.

A radix of 24 means that the valid digits are 0-9 and a-n. Characters beyond ’n’ are not considered valid digits. Consider parseInt("123", 8). This would interpret the string “123” as an octal number, resulting in the decimal value 83 (1 8^2 + 2 8^1 + 3 8^0). Another example is parseInt("ff", 16) which translates to 255 (15 16^1 + 15 16^0). If the input string contains characters that are not valid digits in the specified radix, parseInt() stops parsing at the first invalid character and returns the integer value of the parsed portion of the string. This behavior is essential to understanding why parseInt(null, 24) yields 23.

Featured Snippet: To summarize, parseInt(null, 24) evaluates to 23 because JavaScript first converts null to the string “null”. Then, parseInt() parses “null” with a radix of 24. Since “n” is the first valid digit in base 24 (representing 23) and “u” is not, parseInt() stops at “n” and returns 23.

Infographic here
Practical Implications and Best Practices -----------------------------------------

While the example parseInt(null, 24) === 23 might seem like a purely theoretical curiosity, understanding its underlying principles has practical implications for writing robust JavaScript code. It highlights the importance of being explicit about data types and radix values when using parseInt(). Failing to do so can lead to unexpected results and difficult-to-debug errors. Always specify the radix to ensure that parseInt() interprets the input string as intended. This becomes even more crucial when dealing with user input or data from external sources, where the format of the input string might not be guaranteed.

Here are some best practices to follow when using parseInt():

  • Always specify the radix.
  • Validate user input to ensure it conforms to the expected format.
  • Be aware of JavaScript’s type coercion rules.

These practices will help you avoid common pitfalls and write more reliable code. For example, if you are expecting a decimal number, always use radix 10: parseInt(userInput, 10). According to a Stack Overflow survey, developers who consistently use explicit radix values report fewer issues with parseInt(). Stack Overflow is a great resource for JavaScript best practices. Consider a scenario where you are processing data from a CSV file. If one of the columns is supposed to contain integer values, you might use parseInt() to convert the string values from the CSV file into integers. However, if the CSV file contains a null value, parseInt(null, 10) will return NaN, which can cause issues later in your code. To handle this, you should check for null values before calling parseInt() or use a more robust parsing library that handles different data types gracefully. Additionally, consider the implications of using parseInt() on large numbers. While JavaScript can handle large numbers, parseInt() might truncate or round them, depending on the radix and the length of the string. For handling large numbers, you might want to explore alternative solutions like using the Number() constructor or dedicated libraries for handling big integers.

FAQ

Why does parseInt(null, 24) return 23?
Because JavaScript converts `null` to the string "null", and `parseInt("null", 24)` parses the "n" (which represents 23 in base 24) and stops before "u".
What is the purpose of the radix in parseInt()?
The radix specifies the base of the numeral system used to interpret the string.
What happens if the radix is not specified?
JavaScript attempts to infer the radix, which can lead to unpredictable results.
What are valid radix values?
The radix must be an integer between 2 and 36, inclusive.
Further Exploration and Learning Resources ------------------------------------------

If you’re interested in delving deeper into JavaScript’s type coercion and parseInt() function, there are numerous online resources available. The Mozilla Developer Network (MDN) provides comprehensive documentation on JavaScript’s built-in functions, including parseInt(). MDN Web Docs - parseInt() offers detailed explanations, examples, and browser compatibility information. Additionally, exploring resources like “You Don’t Know JS” by Kyle Simpson can provide a deeper understanding of JavaScript’s core concepts and nuances.

Here are some additional resources to enhance your understanding:

  • Read the ECMAScript specification for a formal definition of JavaScript’s behavior.
  • Experiment with different values and radixes in parseInt() to observe the results.
  • Explore other JavaScript quirks and their explanations.

By actively exploring these resources and experimenting with the code, you can gain a more solid grasp of JavaScript’s intricacies and improve your programming skills. Remember that understanding these seemingly strange behaviors can make you a more proficient and confident JavaScript developer. You can also check out online coding platforms that provide interactive exercises to test your knowledge and reinforce your learning. Continue learning here.1. Understand the basics of parseInt() and its parameters. 2. Learn about JavaScript’s type coercion rules. 3. Experiment with different radix values. 4. Apply these principles to your coding projects.

By understanding the subtle interactions of JavaScript’s type system and built-in functions, we can write better, more predictable code. The seemingly odd result of parseInt(null, 24) === 23 serves as a reminder to always be mindful of data types, radix values, and the underlying mechanics of the language. Embrace these quirks as opportunities for learning and deepening your understanding of JavaScript. Ready to put your newfound knowledge to the test? Try experimenting with different radix values and data types in your own projects. Question & Answer :

Alright, so I was messing around with parseInt to see how it handles values not yet initialized and I stumbled upon this gem. The below happens for any radix 24 or above.

parseInt(null, 24) === 23 // evaluates to true 

I tested it in IE, Chrome and Firefox and they all alert true, so I’m thinking it must be in the specification somewhere. A quick Google search didn’t give me any results so here I am, hoping someone can explain.

I remember listening to a Crockford speech where he was saying typeof null === "object" because of an oversight causing Object and Null to have a near identical type identifier in memory or something along those lines, but I can’t find that video now.

Try it: http://jsfiddle.net/robert/txjwP/

Edit Correction: a higher radix returns different results, 32 returns 785077
Edit 2 From zzzzBov: [24...30]:23, 31:714695, 32:785077, 33:859935, 34:939407, 35:1023631, 36:1112745


tl;dr

Explain why parseInt(null, 24) === 23 is a true statement.

It’s converting null to the string "null" and trying to convert it. For radixes 0 through 23, there are no numerals it can convert, so it returns NaN. At 24, "n", the 14th letter, is added to the numeral system. At 31, "u", the 21st letter, is added and the entire string can be decoded. At 37 on there is no longer any valid numeral set that can be generated and NaN is returned.

js> parseInt(null, 36) 1112745 >>> reduce(lambda x, y: x * 36 + y, [(string.digits + string.lowercase).index(x) for x in 'null']) 1112745