How to Convert String to Integer on Javascript

When writing programs and working with web pages written with Javascript, it is often useful to convert strings to integers. In fact, when you are getting input from the user, you will nearly always place that input in a string. After that, before you can do calculations on the input, you will need to convert that item into a string. Fortunately, the authors of Javascript have made it relatively simple and easy to convert strings to integers.

Instructions

    • 1

      Declare a variable of type string and store a value in it. This value can be obtained from the user or set manually. Use the var statement to declare your string:
      var myString = "String";

    • 2

      Declare another variable that contains a numeric variable. This should also be declared with a var statement:
      var myInt = 0;

    • 3

      Convert the string to an integer value by using the valueOf method of the string object:
      myString.valueOf();

    • 4

      Store this resulting number in a variable for later use:
      myInt = myString.valueOf();

    • 5

      Now use the numerical value of the string where you need it with the myInt variable.

Tips & Warnings

  • Be sure to use the same case in your variable names as Javascript is case-sensitive.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured