parseInt()
global function
JS Home <<
JS Reference <<
parseInt()
Global function that converts a string to an integer.
Description
Parses a string argument with an optional radix and attempts to return an integer.
Syntax
Signature | Description |
---|---|
parseInt(string[, radix]) | Global function that converts a string to an integer. |
Parameters
Parameter | Description |
---|---|
string | The string value you want to parse.
|
radix | The optional radix (number base) that pertains to the string value specified. Although optional for predictable results and clarity always specify a radix. When no radix is entered or is 0 the following is assumed by JavaScript:.
|
Examples
Below are some examples of using the parseInt()
function.
// Parsing string to integers.
var parsedValues = new Array(6);
parsedValues[0] = parseInt('17' 10); // Decimal base
parsedValues[1] = parseInt('10001', 2); // binary base
parsedValues[2] = parseInt('11', 16); // hex base
parsedValues[3] = parseInt(' 17 ', 10); // within spaces
parsedValues[4] = parseInt('17.2345', 10); // decimal
parsedValues[5] = parseInt('abcdef', 10); // invalid
alert(parsedValues);
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 6 - More Maths Functions