﻿ Type Casting ﻿

Main Page

# Type Casting

The
parseFloat()
method works in a similar way to
parseInt()
, looking at each character starting in
position 0. It also continues until the first invalid character and then converts the string it has seen up to
that point. For this method, however, the decimal point is a valid character the first time it appears. If
two decimal points are present, the second is considered invalid and the
parseFloat()
method con-
verts the string up until that position. This means that the string
“22.34.5”
will be parsed into
22.34
.
Another difference when using
parseFloat()
is that the string must represent a floating-point number
in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number
0908
will be parsed into
908
, and the hexadecimal number
0xA
will return
NaN
because
x
isn’t a valid charac-
ter for a floating-point number. There is also no radix mode for
parseFloat()
.
Some examples of using
parseFloat()
:
var fNum1 = parseFloat(“1234blue”); //returns 1234.0
var fNum2 = parseFloat(“0xA”); //returns NaN
var fNum3 = parseFloat(“22.5”); //returns 22.5
var fNum4 = parseFloat(“22.34.5”); //returns 22.34
var fNum5 = parseFloat(“0908”); //returns 908
var fNum6 = parseFloat(“blue”); //returns NaN
Type Casting
It’s also possible to convert values using a process called
type casting.
Type casting allows you to access a
specific value as if it were of a different type. Three type casts are available in ECMAScript:
?
Boolean(value)
– casts the given value as a Boolean
?
Number(value)
– casts the given value as a number (either integer or floating-point)
?
String(value)
– casts the given value a string
Casting a value using one of these three functions creates a new value that is a direct conversion of the
original. This can lead to some unexpected results.
The
Boolean()
type cast returns
true
when the value is a string with at least one character, a number
other than 0, or an object (discussed in the next section); it returns
false
when the value is an empty
string, the number 0,
undefined
, or
null
. The following code snippet can be used to test type casting
as a Boolean:
var b1 = Boolean(“”); //false – empty string
var b2 = Boolean(“hi”); //true – non-empty string
var b3 = Boolean(100); //true – non-zero number
var b4 = Boolean(null); //false - null
var b5 = Boolean(0); //false - zero
var b6 = Boolean(new Object()); //true – object
The
Number()
type cast works in a manner similar to
parseInt()
and
parseFloat()
, except that it
converts the entire value, not just part of it. Remember that
parseInt()
and
parseFloat()
only con-
vert up to the first invalid character (in strings), so
“4.5.6”
becomes
“4.5”
. Using the
Number()
type
cast,
“4.5.6”
becomes
NaN
because the entire string value cannot be converted into a number. If a string
value can be converted entirely,
Number()
decides whether to use
parseInt()
or
parseFloat()
. The
following table illustrates what happens when
Number()
is used on various values:
24
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 24 Free JavaScript Editor     Ajax Editor