toInt

.toInt()

This does the same as 'toNumber', except with a few changes that make it faster. First it will only return a whole number, it will not return a decimal number. Second, it will also return '0' on invalid numbers.

If you need accuracy, use 'toNumber'. Only use this if you know the string is definitely holding a whole number.

If the string does have any decimal places in it's number, then these are lots. No rounding takes place, the decimals are simply discarded.

This will also parse hex values, so for example a string holding '0xFF' will return 255.

str = "34"
num = str.toInt()
console( num ) // outputs 34

str = "123.999"
num = str.toInt()
console( num ) // outputs 123

For speed reasons, invalid numbers are returned as 0.

See also

toNumber