AJAX - The Complete Reference

(avery) #1

558 Part IV: Appendixes


Method Explanation Example
parseInt("string",
[radix])

Converts a string value to an
integer number if possible. If no
number is found in the string
passed or another non-number
type is used, it returns NaN.
The optional radix value can be
set to the base of the desired
conversion. This may be
important if converting from
a leading zero-valued string,
which would be in octal.

var a=parseInt("5");
// 5
var b=parseInt("5.21");
// 5
var c=parseInt("5tom");
// 5
var d=parseInt("true");
// NaN
var e=parseInt(window);
// NaN

parseFloat("string") Converts string value into
a floating point number if
possible. When passed non-
strings or if no floating point is
found in the passed string, it
returns NaN.

var x = parseFloat("3.15 ");
// x = 3.15
var y =
parseFloat("74.5red-dog ";
// y = 74.5
var z = parseFloat("TAP ");
// z = NaN
var q = parseFloat(window);
// q = NaN
+ value Converts value into number if
possible, given type conversion
required for prefix plus operator.

var x = + "39";

Number(value) Converts value into a number if
possible, otherwise NaN.

var x = Number(5);
var y = Number("5"); //5
var z = Number("F"); //NaN
String(value) Constructor that turns the
passed value into a string type.

var x = String(true);
var y = String(5); //"5"
Boolean(value) Constructor that turns the
passed value into a Boolean
type.

var x = Boolean(true);
var y = Boolean(1); /* true
*/
var z = Boolean("");/* false
*/
!!value Converts value to its Boolean
representation because of
implicit convert of! operator.

var x = !!(true);
var y = !!(1); // true
var z = !!""; // false
Obj.valueOf() The method that is called to
convert an object to a primitive
value. Rarely called directly.

alert(window.valueOf());

Obj.toString() A method to present an object
in a string form. Similar to
valueOf() though often
overridden by developers.

alert(window.toString());

TABLE A-7 Type Conversion Methods
Free download pdf