JavaScript

Javascript String to Int

Javascript is a language of the web and managing data is an important aspect of any programming language. We often need to manipulate or manage variables according to our needs. Sometimes we need to perform arithmetic operations so, we can’t do that with strings. We need integers to do that.

Since Javascript is a language of the web now. Speed optimization has become very important in this era. We have to think and manage every single byte if we can. We must know and care about memory because the strings take more memory than integers. We need to keep things very simple. But, what if we have to perform some arithmetic operations. If, variables are in string type. Do we have to reinitialize the variable with integer type? Of course not! It will even take more memory. But, what if we have a function that will convert or parse the string into the integer and we can perform our tasks. So, in this article, we are gonna see that how can we convert or parse a string into the integer using the parseInt() function.

The parseInt() is a function to which we can pass a string as an argument and it will return us an integer if it exists.

This function returns NaN(Not a Number). If, no number found in that string. This function also returns NaN if there exists any character before the number.

Syntax

Let’s take a look at the syntax of the parseInt() function.

parseInt(value [, base]);

Here,

Value is the string that we want to parse into the integer.

And the base is the base number of the provided string to which we want to convert into a decimal number. It is an optional value.

Let’s have look at a couple of examples to understand more clearly.

Examples

parseInt("34"); // 34</td>

Now, let’s try to give a float number.

parseInt("34.53"); // 34

As you can see. It only prints the 34.

Let’s try to put a space before or after the number.

parseInt(" 34 "); // 34

It worked fine.

But, if we put any character before the number.

parseInt("the 34"); // NaN

It prints NaN(Not a Number). The same applies to the empty string.

Pro tip

Now, what if we try to give the base number along with the value. Like, the base of the binary number system is 2.

parseInt("34", 2); // NaN

Ok, since 3 and 4 are not the numbers of a binary number system. It prints NaN.

Now if we provide it a true binary number. It should print the decimal number against that binary number.

parseInt("10011011", 2); // 155

Here comes an interesting thing about this function. Like, if we keep on providing the binary number 0’s and 1’s. It will keep on converting that number into the decimal number system. But, when we start to give a non-binary number system. It will stop right there and won’t convert any further. But, until we keep on giving the binary numbers. It keeps on converting.

parseInt("100110113432", 2); //155

Alright! We can also do the same tasks with the Octal number system and Hexadecimal number system using the parseInt() function.

Conclusion

In this article, we have learned that how can we use the parseInt() function to convert the string into an integer. We have also learned about some exceptional cases of the parseInt() function and how does it help in converting the number systems as well. I hope this article was beneficial and helpful for understanding the conversion of strings into integers. So, keep on learning javascript with linuxhint.com.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.