Spot parseInt Radix Issue
Question
This code gives unexpected results for some inputs. What is the issue?
Code
const result = parseInt("08");
console.log(result);
Hint
Always specify the radix (base) to avoid ambiguity.
Explanation
Without a radix parameter, parseInt may interpret strings with leading zeros as octal (base 8) in older environments. parseInt("08") could be 0 (since 8 is not a valid octal digit) instead of 8. Always pass the radix: parseInt("08", 10) guarantees base-10 parsing and returns 8. Modern JavaScript defaults to base 10 for strings starting with 0, but specifying the radix is still best practice.
Related Resources
Related Lessons
- JavaScript Data Types
Learn about parseInt.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.