JavaScript
Programming Concepts
Numeric Conversion
Software Debugging
Coding Issues

Why does parseInt(1/0, 19) return 18?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In JavaScript, the parseInt function is used to convert a string into an integer of the specified base (or radix). The syntax for parseInt is:

javascript
parseInt(string, radix)

Here, string is the value to parse and radix is the base of the numerical system used. If the radix is omitted or is 0, JavaScript assumes the following:

  • If the string begins with "0x" or "0X", radix is assumed to be 16 (hexadecimal),
  • If the string begins with "0", radix is 8 (octal) or 10 (decimal). Note that some browsers default to 10 (decimal) even if it begins with "0" due to recent ECMAScript standards,
  • If the string begins with any other value, the radix is 10 (decimal).

However, an interesting behavior occurs when you call parseInt(1/0, 19). To understand why parseInt(1/0, 19) returns 18, let's break down the process:

Understanding 1/0

In JavaScript, dividing by zero (1/0) results in Infinity. Thus, the expression 1/0 evaluates to Infinity.

Converting Infinity to a String

When Infinity is passed to parseInt, it first gets converted to the string "Infinity". Therefore, parseInt('Infinity', 19) is really what is being evaluated.

How parseInt Works with "Infinity" and Base 19

When parsing the string "Infinity" with radix 19, parseInt checks each character in the string and determines if it is a valid digit in the specified radix:

  • I - Not a valid digit in base 19.
  • n - Valid, represents 23 in base 19, but since 23 is outside the allowable range of 0 to 18 for base 19, it stops processing.
  • f - The first character to consider is "f", which in base 19 is a valid digit and represents 15.

Therefore, the first valid group of characters in the base-19 number system is "I", which does not contribute any value because it's not valid. The parseInt function then moves to the next characters but ignores them until it finds "f", interprets it in the context of base 19, returning 15. However, there's still another char "i".

  • i - In base 19, "i" represents 18.

Hence, the final result comes from converting "i" in base 19 to a decimal, which results in 18.

Example Illustration

javascript
console.log(parseInt("Infinity", 19));  // Outputs: 18
console.log(parseInt("f", 19));         // Outputs: 15
console.log(parseInt("i", 19));         // Outputs: 18

Summary Table

Here is a summary of the key points and the role they play in why parseInt(1/0, 19) returns 18:

FactorRole or ValueDescription
1/0InfinityJavaScript division by zero results in Infinity.
Converting to String"Infinity"Infinity is passed to parseInt as "Infinity".
Radix 19Base-19 numeric systemSpecifies the numeral system to use for parsing.
Parsing "Infinity"Interpretation stops at 'i'"f" is skipped because "I" was not valid, "i" interpreted as 18.

Additional Details on parseInt and Radix Usage

Understanding the importance of specifying a radix in parseInt is crucial to avoid unexpected results. Always explicitly define the radix if the interpretation of the string may vary. This approach avoids ambiguity, especially when dealing with user input or data from multiple sources. In practical terms, parseInt usage should be clear about the numeral base to prevent logic errors in the software.


Course illustration
Course illustration

All Rights Reserved.