ECMA-262 Core: JavaScript Data Type Conversions



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site






Introduction And Review

Each JavaScript primitive data type has a set of valid values associated with the type. We have learned that via the process of weak typing, JavaScript determines which data type is appropriate for a variable based on the value of the literal token that is assigned to the variable.

Type conversions occur when a value that is appropriate for one given type is used in a context appropriate for a completely different type. For example, if a string is used in a numeric context, JavaScript will attempt to convert it to a number.



Automatic Type Conversions

In JavaScript, type conversions can occur automatically. JavaScript knows the complete set of valid values that are appropriate for all primitive data types. But when a value that is appropriate for one data type is used in context with a second data type, JavaScript will attempt to convert the value to be suitable for the second data type. For example, passing a number into a method that requires a string will result in an attempt at automatically converting the number to a string to satisfy the requirements of the method. Simple assignments will cause type conversions:

var xyz = "44"; // xyz is dynamically typed as a string xyz = 44; // xyz converts to a Number type

The following table show the complete set of type conversion possibilities where values are used out of normal context.



Automatic Type Conversions
Literal Value Used out of Context Comment
String Number Boolean Object
Footnote A cell entry of "native" indicates that the value is a native value for the associated type; no type conversion required.
undefined "undefined" NaN false error A TypeError exception is thrown when used in object context.
null "null" 0 false error A TypeError exception is thrown when used in object context.
true "true" 1 native Boolean
false "false" 0 native Boolean
NaN "NaN" native false Number Numbers in Boolean context: false when value is either NaN or zero; otherwise true.
0 "0" native false Number
Infinity "Infinity" native true Number
-Infinity "-Infinity" native true Number
Other Numbers "the number" native true Number
Empty String native 0 false String where string length is zero
String native numeric true String In Number context; a string with an invalid number converts to a NaN value for the Number type.
Object toString() NaN true native When an object's value is used in the context of a String, the toString() method will attempt to convert the type to a string.


Explicit Type Conversions

The three primitive data types (Boolean, Number and String) each have an associated object (or class). The objects ( Boolean(), Number(), and String() ) can be used with a constructor or as a type conversion function.

The second example is our focus here. When you specify a value of one primitive type into a function of a different type, the result will reflect a value that has undergone a type conversion. The original type has been cast (or converted) into a value appropriate of the second type.



Explicit Type Conversions
Conversion Statement Result Comment
Number converted
   to Boolean
var boo = Boolean(1); true a non-zero number converts to true
Number converted
   to Boolean
var boo = Boolean(0); false a zero converts to false
Number converted
   to Boolean
var boo = Boolean(NaN); false a NaN converts to false
String converted
   to Boolean
var boo = Boolean("string"); true a string with at least one character converts to true
String converted
   to Boolean
var boo = Boolean(""); false a empty string converts to false
Null converted
   to Boolean
var boo = Boolean(null); false a null converts to false
Undefined converted
   to Boolean
var boo = Boolean(undefined); false undefined converts to false
Null converted
   to Number
var num = Number(null); 0 a null converts to zero
Undefined converted
   to Number
var num = Number(undefined); NaN undefined converts to NaN
Boolean converted
   to Number
var num = Number(true); 1 a boolean true converts to one
Boolean converted
   to Number
var num = Number(false); 0 a boolean false converts to zero
String converted
   to Number
var num = Number("0"); 0 a string with value of zero converts to integer zero
String converted
   to Number
var num = Number("99"); 99 a string with value of 99 converts to integer 99
String converted
   to Number
var num = Number("9.9"); 9.9 a string with value of 9.9 converts to a float of 9.9
String converted
   to Number
var num = Number("9.9.9"); NaN a string with value of 9.9.9 will not convert to a number and results as NaN
Null converted
   to String
var string1 = String(null); "null" a null type converts to a string "null"
Undefined converted
   to String
var string1 = String(undefined); "undefined" undefined type converts to a string "undefined"
Boolean converted
   to String
var string1 = String(true); "true" boolean type converts to a string "true"
Boolean converted
   to String
var string1 = String(false); "false" boolean type converts to a string "false"
Number converted
   to String
var string1 = String(9.9); "9.9" number type converts to a string "9.9"


Type Conversion Methods

JavaScript has built-in methods and functions for converting data types. The Number class contains methods ("toString(), toFixed(), toExponential()" that converts numbers to strings. var num = 44; var string1 = num.toString(); //string1 = "44"
The Global object contains two methods for converting strings to numbers. The "parseInt()" method converts strings to integers and the "parseFloat()" method converts strings to floats.

Type Conversions Tricks

There are other subtle ways to force type conversions. You can concatenate any value to an empty string to force a conversion to a string. var newstring = anyvaluevar + "";
Subtract zero from a value to force it to a number. var newnum = anyvaluevar - 0;
Use the ! operator twice to force a value to boolean. var newboo = !!anyvaluevar;



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson