Number Bases and Leading Zeros
Join the Discussion
Everyone is familiar with the base 10 (or decimal) way of counting where we have ten separate symbols to represent different values - 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. To get values greater than 9 (the largest number having a separate symbol) we start combining our symbols together and use 10 to represent the number one bigger than 9.
At this point you are probably wondering what the above two paragraphs have in common and why I have explained something so obvious as our numbering system the way that I have. The answer is that the decimal numbering system is not the only possible numbering system and is in fact not the one that computers themselves use.
While we prefer to count using the decimal system, computers prefer to count using the binary (base 2) system. Binary has only two sysmbols to represent different values - 0 and 1. To get values greater than 1 we combine sysbols together the same way that we do in our decimal system and so in binary 10 represents the number one bigger than 1 (which is represented with 2 in our decimal system). Even 1001 is not a very big number in binary as it can still be represented with just one symbol in decimal since it is the binary equivalent of 9.
The biggest problem with binary numbers is that we can end up with large combinations of 1s and 0s that can take us a long time to work out what the decimal equivalent is so that we will know what the number really represents. To help us to solve this problem we introduced two further number bases that computers can easily convert to from binary that are also easier for us to work with. These two number bases are octal (base 8) which uses 0, 1, 2, 3, 4, 5, 6, and 7 and hexadecimal (base 16) which uses 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F (allowing numbers up to 15 to be represented by a single symbol). Computers can easily convert binary to one of these because it is simply a matter of taking three or four binary digits (usually abbreviated to bits) and representing them as the one equivalent symbol in the octal or hexadecimal system. Since octal and hexadecimal numbers are approximately the same length as the equivalent decimal numbers they are much easier for us to work with than binary.
When we use parseInt() we can specify the number base that we want the resulting number to be in by specifying the number base in the second (optional) parameter. If we don't specify a number base in that parameter then the number base will be worked out from the content of the number itself. With parseint('09') we are asking the function to extract the number from this string in octal. Since 9 is not a number in the octal numbering system the function drops it and returns the number portion preceding it giving us 0. Of course the function treats 01 through 07 as octal numbers as well but since these octal numbers are identical to their decimal equivalents we don't notice when these numbers are returned that the function is using the wrong number base. To force the parseInt function to extract decimal numbers from the string all we need to do is to pass 10 in the second parameter. The following will return the 9 that we expect./p>