parseInt (JavaScript)

Parses a string and returns an integer of the specified radix or base.

Defined in

Top-level functions (JavaScript)

Syntax

parseInt(string, radix)
Parameter Description
string A string that represents an integer of the specified radix. Specify digits greater than 9 as letters.
radix (Optional) The radix of the integer. Defaults to 10.

Usage

If string cannot be converted to an integer in the specified radix, the result is the value NaN. See isNaN (JavaScript).

Examples

This example creates integers from strings in radix 10, 8, and 16, and tests them to make sure they are valid numbers.
function p(stuff) {
	print("<<<" + stuff + ">>>");
}

var i = parseInt("25");
if (isNaN(i))
	p("i is not a number");
else
	p("i = " + i);
// i = 25
	
var i = parseInt("25", 8);
if (isNaN(i))
	p("i is not a number");
else
	p("i = " + i);
// i = 21

var i = parseInt("25", 16);
if (isNaN(i))
	p("i is not a number");
else
	p("i = " + i);
// i = 37

var i = parseInt("25x");
if (isNaN(i))
	p("i is not a number");
else
	p("i = " + i);
// i is not a number

var i = parseInt("ff", 16);
if (isNaN(i))
	p("i is not a number");
else
	p("i = " + i);
// i = 255