HTML and CSS Reference
In-Depth Information
Figure 5.20
The user is prompted to enter a number.
Figure 5.21
The user is prompted to enter another number.
Figure 5.22
The result is displayed.
This function converts a string to a number. It starts parsing at the beginning of the
string and returns all integers until it reaches a noninteger and then stops parsing. If the
string doesn't begin with an integer,
NaN
(not a number) is returned. For example,
par-
seInt(“150cats”)
becomes
150
, whereas
parseInt(“cats”)
becomes
NaN
. However, if the
string begins with a leading 0x or 0X or a leading 0, then JavaScript assumes you want
the 0x to represent the beginning of a hex value and a single 0 to represent the start of
an octal value. You can also use octal and hexadecimal numbers. In the two-argument
format, the first argument to
parseInt()
is a string containing a number base (radix)
ranging from 2 to 36. The default is base 10. In the statement,
parseInt(“17”, 8)
, the
result is
15
. The first argument is the string to be parsed and the second argument, 8, is
the number base of the number (here, octal 17). The value returned is decimal
15
. Refer
to Tables 5.16 and 5.17.
FORMAT
parseInt(String, NumberBase);
Default base is 10
parseInt(String);