# Example 7.12: Built-in Functions--parseInt

This page uses a built-in function, parseInt(form.txtNum.value,nBase), to return an integer value. It examines a string, then extracts a number from the string assuming that the number is in the specified Radix (base). Then it converts the number to decimal. If it's unable to find a number it either returns NaN, or, if the browser is a JavaScript 1.1 browser (Navigator 2, some beta versions of Navigator 3 , and Internet Explorer 3), it returns 0 instead of NaN. (JavaScript 1.1 browsers running on Solaris or Irix will return NaN.)

Type text into the first text box, select the Radix of the number you typed into the text box, then click on the button to see the result.

Remember the data from a form is a string. This function converts a string to a number, if it can. It looks at the string, then tries to extract a number in the specified base from the string, then convert it to decimal.

Select a radix (base) you want to convert to--10 (decimal), 2 (binary), 8 (octal), 16 (hexadecimal):
Type a number:

These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JavaScript"> <!-- function GetInteger(form) { var nBase if (form.cmbRadix.selectedIndex == 0)nBase = 2; if (form.cmbRadix.selectedIndex == 1)nBase = 8; if (form.cmbRadix.selectedIndex == 2)nBase = 10; if (form.cmbRadix.selectedIndex == 3)nBase = 16; form.txtInteger.value= parseInt(form.txtNum.value, nBase) } //--> </SCRIPT> Later in the Web page the function is called from this form: <FORM> Select a radix (base) you want to convert to--10 (decimal), 2 (binary), 8 (octal), 16 (hexadecimal): <select name="cmbRadix" size="4"> <option>Binary (Base 2) <option>Octal (Base 8) <option selected value>Decimal (Base 10) <option>Hex (Base 16) </select><BR> Type a number: <INPUT TYPE="text" NAME="txtNum" SIZE=12><BR> <INPUT TYPE="button" VALUE="Result" onclick="GetInteger(this.form)"> <INPUT TYPE="text" NAME="txtInteger" SIZE=20> </FORM>