0

If I execute parseInt('111AAA') we get the output '111'. In javascript why does parseInt work as its mentioned in the MDN docs.

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

I've started to pick up JS and have a background working in Java and C. Shouldn't it be giving error if an alphanumeric value is supplied to it. What is the reasoning behind this behaviour ? Is there any other language that adopts this ?

Edit: Its been pointed out that similar functionality exists in C by @jabaa https://en.cppreference.com/w/cpp/string/basic_string/stol

slick
  • 65
  • 8
  • 2
    _"Is there any other language that adopts this ?"_ Yes, C, C++ and probably many others ;-) https://en.cppreference.com/w/cpp/string/basic_string/stol See the example with `"31337 with words"`. It's parsed to `31337`. Most "weird" things about JavaScript I've read on Stack Overflow are very common in many other programming languages. –  Jun 16 '21 at 08:28
  • 1
    Welcome to Javascript. your question is a bit weird, why is it working as described on the docs? why would it not? – monxas Jun 16 '21 at 08:30
  • 1
    [Is asking “why” on language specifications still considered as “primarily opinion-based” if it can have official answers?](https://meta.stackoverflow.com/questions/323334/is-asking-why-on-language-specifications-still-considered-as-primarily-opinio) – Ivar Jun 16 '21 at 08:37
  • 1
    *Shouldn't it be giving error if an alphanumeric value is supplied to it.* Why would you parse it in the first place, if that was the case? – JavaScript Jun 16 '21 at 08:37
  • 1
    If you want a more strict version use `Number('111AAA')`, this will return `NaN`.. – Keith Jun 16 '21 at 08:45
  • @monaxs I raised this question because there was a really good answer to one of my queries about NaN. https://stackoverflow.com/questions/2801601/why-does-typeof-nan-return-number . Similarly I was searching for an answer like that regarding my question. – slick Jun 16 '21 at 08:57
  • "Why does work like it's supposed to" do you... do you expect it *not* to? I don't see why one would expect something not to work the way it is intended in a language. – TylerH Jun 16 '21 at 20:49

1 Answers1

4

As the name suggests, parseInt as a parser, not a converter. While a converter takes a string as a whole and tries to make sense of it, a parser consumes the input symbol by symbol and stops at the first symbol it cannot process. In javascript, the converter for numbers is Number:

console.log(Number('123foo'))
console.log(parseInt('123foo'))

In C, atoi, strtol and similar functions are all parsers, not converters.

georg
  • 211,518
  • 52
  • 313
  • 390