Today we’re going to look at the parseInt() method. This is really useful when you need an integer but all you have is a string.
There are two parameters up there:
- radix – In simple terms, this sets the numeric base system fof the string you gave it. So, we normally use base 10 (decimal), computers use base 2 (binary) and base 8 (hexadecimal). Generally, you’ll set this to 10, for decimal. Note that neither of these values are shown as optional – always set the radix, because if you don’t, you could get some really strange results.
parseInt() will take the string and convert it to an integer. If the string can’t be converted (it’s a bunch of letters, etc.) it will return NaN. When it returns an integer, it will convert the string per the radix – what base you have chosen to specify in your second argument above.
Finally, the reverse method (convert the integer back to a string), you would call:
So let’s look at some of MDN’s examples. Every one of these will return a 15! You may want to review converting numbers back and forth from octal to decimal, hex to decimal, etc, or a lot of these will be…strange to your eyes.
parseInt(” 0xF”, 16);
Okay, so here we have a leading space that’s ignored, a 0x that indicates that this is probably hex, and we’re specifying that the string is indeed base 16, or hex. 0xF in decimal is 15.
Same as above, but we left the 0x off. We said we wanted it to be read as hex, so parseInt() was able to figure it out anyway.
We’ve specified a radix of octal, or base 8. In base 8, 17 is not 17….it’s 15!
We’ve specified a radix of octal, or base 8. In base 8, 021 is not 21….it’s 15!
We’ve specified a radix of base 10. Now, this method is parseInt, so we’re going to get an integer back, not a float, so we get 15, not 15.99.
This one is tricky. It parses the 15 in base 10, like we told it to. So why don’t we get back 15,123? Well, because the comma is NaN and so the rest of the string is ignored!
We specified base 16, or hex. We get back 15 again, becausew it sees the F, parses that, then runs into X, which is NaN, and it ignores the rest of the string.
We specified base 2 (binary). In binary, 1111 is equal to 15 in decimal.
Wait! That’s an equation! Yep, and parseInt() decides that since we told it that we want this interpreted as base 10, it will take the 15, run into an * which is totally NaN, and return just the 15.
I bet you already figured out that e is NaN, so yeah, just the 15 is returned.
Yep, px are both NaN. Just 15 is returned.
Wait! What? Base 13??? Well, parseInt90 is smart. It converts it from base 13 and we get…15!
How about some bogus numbers.
Right. There’s no H in octal, so it doesn’t even try.
Binary numbers are 0 and 1. No binary number is going to have a 5, 4, or 6, silly human.
What if we want a negative number? Add a negative sign (-) in front. All of these will give you -15, and since they are similar to the ones giving you +15 above, I won’t go through them in detail:
Here’s another hex example, this time returning 224:
Finally, there are ways to make the parse much more strict. The one below uses a regular expression. I’m not going to go through this in detail, but I’m including it here for completeness.
Remember, always set your radix!!! See you next time!