Body:
I’m trying to convert a string to an integer in JavaScript using the parseInt() function. It works as expected for most inputs, but returns NaN when I try to parse “08”. Here’s the code I’m using:
javascript
Copy code
var result = parseInt(“08”);
console.log(result); // Outputs: NaN
From my understanding, parseInt() should convert a string to an integer. It works correctly when I test it with other strings like “07” or “09”. Why does it return NaN specifically for “08”?
What I’ve tried:
Using Number(“08”) instead, which correctly returns 8.
Reading the MDN documentation on parseInt(), but I didn’t find specifics about this case.
Could this be related to how JavaScript handles string parsing, or is there something else I’m missing?
garrab eya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.