JavaScript – Convert String to Int in JavaScript

How to Convert a String to int in JavaScript Language?

Often, in Javascript a number is input as a string. To use it for any mathematical operation, we have to convert the string to int. So, How to Convert String into Integer in javascript? At first, we can convert the string to int in Javascript or use built-in functions. At the same time, we can write our own function. So let’s solve it.

Manual Conversion

JavaScript Program Example

An example of converting a string to int in JavaScript.

Output:String To Int in JavaScript custom function

Description of the program

The function char2decBase converts char c to decimal number in base numberradix.

The function string2int converts the stringNumber to int number in base numberradix. The base range is from 2 till 36.

  1. Check if the string is not empty
  2. Check if a number is within a range [2, 36]
  3. Check the sign
  4. The String to int in base 10 conversion loop
  5. Returns the result, if the number is negative then change the sign of the number

Convert a string to int in Javascript using the parseInt() Function

The parseInt() function parses a string and returns an integer.



string – is the string representation of an integral number. In case this argument is not a string, it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.
radix – an integer between 2 and 36 that represents the numeral system to be used.

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with “0x”, the radix is 16 (hexadecimal);
  • If the string begins with “0”, the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

Return Value

This function returns integer value of string on success. If the first character can not be converted to a number, parseInt() returns NaN.


  • Only the first number in the string is returned;
  • Leading and trailing spaces are allowed;
  • Older browsers will result parseInt(“010”) as 8 (octal), because older versions of ECMAScript, (older than ECMAScript 5), uses the octal radix = 8 as default when the string begins with “0”. As of ECMAScript 5, the default is the decimal radix  = 10.


Output:String To Int in JavaScript parseInt() function


JavaScript – Wikipedia, the free encyclopedia
JavaScript parseInt() Function
JavaScript string-to-number conversion
Online Javascript Editor

3 thoughts on “JavaScript – Convert String to Int in JavaScript”

  1. I will make a few remarks which I found in your javascript function:
    1) I think that it is necessary to check if the JavaScript variable is type of string. You can use the JavaScript typeof operator (JavaScript operator that returns a string):
    if(typeof str != ‘string’) {
    str = str.toString();
    2) I see you use JavaScript try catch, I think it would be better to return NaN;
    3) I think it’s better to use JavaScript for loop.

  2. Hello everyone! How to enable javascript in internet explorer? I tried to run this example from internet explorer, and it seems to be that the javascript is disabled.
    Another question, how to call javascript function on button click? I’m a beginner and I don’t know how to do that, I want to call javascript function on button click.

    1. It is very strange that javascript is disabled on your browser, to enable javascript in IE take a look at this page
      Regarding the call javascript function on button click, there are a few ways to handle events with HTML like:

      1: There’s defining it in the HTML:

      <input id="click_button" type="button" value="Click button" onclick="callMyFunction();" />

      2: There’s adding it to the DOM property for the event in Javascript:
      //- Using a function pointer:
      document.getElementById(“click_button”).onclick = callMyFunction;
      //- Using and anonymous function:
      document.getElementById(“click_button”).onclick = function () { callMyFunction(); };

      3: And there’s attaching a function to the event handler using Javascript:
      var the_button = document.getElementById(“click_button”);
      if (the_button.addEventListener) {
      the_button.addEventListener(“click”, callMyFunction, false);
      } else if (the_button.attachEvent) {
      the_button.attachEvent(‘onclick’, callMyFunction);


      <!DOCTYPE html>
      <title>Button Test Example</title>
      function callMyFunction() {
      <input id="click_button" type="button" value="Click button" onclick="callMyFunction();" />
      This html button call javascript function “callMyFunction()” and as a result it alerts the “Hello!” message.

Leave a Reply

Your email address will not be published. Required fields are marked *