# Need help understanding parseInt();

Hello all, I’m in need of some assistance in understanding the parseInt function in JavaScript. The further I’m digging into these exercises, I’m coming across this function more frequently, and no matter how much I’ve read on the topic, I can’t fully grasp how it works entirely. Is there any resources you can recommend, or anyone who could provide a simplified explanation of the function? Thanks so much in advance!

How parseInt() works is less important than what it does: it takes a string, and returns a number. So if you hand it “123”, then it returns 123. If you give it “25xyz345”, it returns 25 (it stops at the first non-number part it sees).

The second argument to parseInt is interesting: it’s the radix, which is the base of the number system you’re trying to parse. For example, if you want to parse binary (base 2), then `parseInt("101", 2)` would return 5, rather than 101. Or `parseInt("ff", 16)` would return 255 (since “ff” is hexidecimal for 255).

The easiest way to learn a function is to play with it, using something like repl.it. Just plug in different values into parseInt until you get an intuition of how it works. It’s the same thing I do for any unfamiliar function, and it’s an awesomely useful technique for learning anything – just try it out.

5 Likes

Thanks so much! That is actually real useful information, and I appreciate that basic break down. I’m starting to gradually understand that, I suppose that’s where my confusion formed, was with the radix, and not so much the function itself. I’ll be sure to fiddle around with it, thanks again!

Nice explaination @chuckadams. just to add, as the name inplies `parseInt` it will always return an integer if you ever want to return numbers with decimal, you may want to consider `parseFloat`.

1 Like