The function you are looking for is parseInt. Here’s how you use it.
var my_string; var my_int; my_string = "10"; // Convert my_string to an integer // This is the only important line for this solution my_int = parseInt(my_string); // my_int is now equal to 10 and of type number (js integer) document.write("my_int is equal to: " + my_int); document.write("<br />"); document.write("my_int is of type: " + (typeof my_int));
And there you have it… if you run this you get the following output:
my_int is equal to: 10 my_int is of type: number
Hopefully that helps some of you. Enjoy.