
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
72
|
Chapter 3: Data and Datatypes
Converting to a Boolean
When we want to convert a datum to a Boolean, we can use the global Boolean()
function, which uses similar syntax to the String() and Number() functions. For
example:
Boolean(5); // The result is true
Boolean(x); // Converts value of x to a Boolean
Don’t confuse the global Boolean() function with the built-in class constructor of the
same name. Both are described in the Language Reference.
Conversion Duration
All type conversions performed on variables, array elements, and object properties
are temporary unless the conversion happens as part of an assignment. Here we see a
temporary conversion:
var x = "10"; // x is a string.
y = x - 5; // y is now 5; x's value was temporarily converted to a number.
trace(typeof x); // Displays: "string"; the conversion was temporary because
// it occurred incidentally while evaluating an expression.
Here we see a permanent conversion that is the result of an assignment:
x = "10"; // x is a string.
x = x - 5; // x is converted permanently to a number.
trace(typeof x); // Displays: "number"; the conversion was permanent because
// it occurred as part of an assignment.
Determining the Type of an Existing Datum
To determine what kind of data is held in a given expression before, say, proceeding
with a section ...