parseInt Function

Top  Previous  Next

Parses a string into an integer.




string A string to be parsed and converted to an integer.

Return Value

The value of the parsed integer, or NaN (not a number) if string does not begin with a valid integer.


parseInt stops parsing if it encounters a character that is not a valid part of an integer. If string starts with a valid integer, parseInt will return that integer, ignoring extra characters. If string does not start with a valid integer, parseInt returns NaN. You can test for NaN using isNaN.


parseInt("15cm") // returns 15
parseInt("xyz") // returns NaN
parseInt("12.81") // returns 12

See Also

isNaN, parseFloat, parseNumber