parseInt() method : Type Conversion « Number Data Type « JavaScript Tutorial






The parseInt() method starts with the character in position 0 and determines if this is a valid number

If it isn't, the method returns NaN and doesn't continue.

If it is valid, the method goes on to the character in position 1.

This process continues until a character isn't a valid number.

parseInt() takes the string up to that point and converts it into a number.

parseInt("1234AAA") returns 1234 because it stops processing one it reaches the character A.

Any number literal contained in a string is also converted correctly.

The string "0xA" is properly converted into the number 10.

The string "22.5" will be converted to 22, because the decimal point is an invalid character for an integer.

var iNum1 = parseInt("1234blue");
var iNum2 = parseInt("0xA");
var iNum3 = parseInt("22.5");
var iNum4 = parseInt("blue");








5.8.Type Conversion
5.8.1.Type Conversion
5.8.2.Type-conversion adheres to the following rules
5.8.3.Converting to a String
5.8.4.Using Number's toString() method in radix mode
5.8.5.Converting to a Number
5.8.6.parseInt() method
5.8.7.parseInt() in radix mode
5.8.8.If decimal numbers contain a leading zero, it's always best to specify the radix as 10 so that you won't accidentally end up with an octal value.
5.8.9.parseFloat() method
5.8.10.Type Casting
5.8.11.Casting to Boolean value
5.8.12.Casting to Number
5.8.13.Casting type to string
5.8.14.parseInt("33.00")
5.8.15.parseFloat("1.23e-2")
5.8.16.parseFloat("1.45inch")