Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

    Parse a string to an integer

    Basarat Ali SyedBasarat Ali Syed

    A common interview question is to write
integer e.g. "123" => 123.
is commonly
 atoi because

    In this lesson we cover the proper way to do this in JavaScript which is parseInt along with implementing it using basic ascii math.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:01 A common interview question is to write a function that takes a string, parses it, and returns its integer value. For example, the string 123 to its integer value 123. We start by creating our atoi function that takes a string and returns a number.

    00:21 JavaScript has a function called parseInt that can do this integer parsing for us. Let's run through a few example of this built-in functions behavior. For simple ints, we get an int. For ints with a negative sign, we get a negative int.

    00:40 When it encounters a character that is not numeral, it ignores it and all succeeding characters. It still returns a integer value parsed up to that point so far -123 extra, we'll still get -123. You can use this fact to easily ignore decimal positions as demonstrated in this example.

    01:03 Finally, if the string does not start with the decimal numeral, it returns NaN. That is, "Not a Number." If you go ahead and run the example, the output matches our expectations. Note that you can customize the behavior of parseInt quite easily by wrapping it in a function.

    01:23 For example, parseInt silently ignoring invalid trading characters can be considered a behavior you want to change. You can easily implement a more inaudible purpose function with a simple regex test that only allows strings containing things that we support.

    01:40 For example, a waiting sign followed by any number of decimal digits. In all other cases, it will return NaN. Now, all imperfect inputs to a function will return NaN, and we getting verify by running our example.

    02:00 With practical JavaScript training out of the way, when this question is commonly asked, the interviewer might specify that you are not allowed to use parseInt, or any other built-in number parsing functionality. They essentially want to see if you can write a function that is similar to how parseInt would be implemented internally.

    02:21 First, we note down the corrective point for zero. This will allow us to convert each decimal digit from a string to its number value. The next thing we need to do is to parse out the sign. We can store this in the sign multiplier.

    02:39 Next, we create an accumulator for the integer result of parsing the digits. We go through the string left to right. At each point, we update the accumulator as the sum of the current accumulator multiplied by 10, and the number value of the current character which we get from its distance from the zero code.

    03:00 Finally, we return the accumulator multiplied by the sign. If you run through the example with binary inputs, you see that it works as expected. This answer shows your understanding of basic math, like the fact that digits represent powers of 10. This solution also shows an understanding of the fact that string digits represent code points that can be inaudible to get their integer representation.

    03:29 The interviewer might additionally ask you about things that can go wrong in this implementation. A good one worth mentioning is that we are assuming that the string only has valid digits. They might then ask you to fix that issue, and you can simply check the result of this difference to be in the range zero to nine.