JavaScript/Notes/TypeConversion: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
(Created page with "There are 5 primitive types, which are associated with the various primitive values. Null: The value null. Undefined: The value undefined. Number: All numbers, su...")
 
No edit summary
Line 1: Line 1:
There are 5 primitive types, which are associated with the various primitive values.
There are five primitive types in JavaScript: Null, Undefined, Boolean, String, Number.  


    Null: The value null.
Various operations in JavaScript require conversion to and from primitive values.
    Undefined: The value undefined.
 
    Number: All numbers, such as 0 and 3.14. Also NaN, and Infinity.
=== Converting to Boolean ===
    Boolean: The values true and false.
When evaluating the expression of an if statement the Javascript interpreter will type-convert the result of that expression to boolean in order to make its decision. Also various operators internally type-convert their operands to boolean in order to determine what action to take. These include the logical operators like AND (&&), OR (||) and NOT (!). The NOT operator type-converts its operand to boolean and if that value is boolean true it returns false and if false it returns true. As the result of a NOT operation is a boolean value that is the inverse of the type-converted true-ness of its operand, two NOT operations together will return a boolean value that is equivalent to the result of type-converting the operand to boolean.
    String: All strings, such as "foo" and "".
 
=== Converting to String ===
When either operand is a string, the concatenation is performed.
 
Whenever the <code>+</code> operator is used, the operands must be converted into primitive values. First, the interpreter calls the object's valueOf to get a primitive value. If the result is primitive, then that value is used. '''Example:'''
 
<source language="javascript">
var o = { valueOf : function() { return 1; }
o + 1; // 2.
</source>
Otherwise, the object's toString is called.
<source language="javascript">
var o = { toString : function() { return "1"; } }
o + 1; // "11".
</source>
 
=== Converting to Number ===
Converting strings is a very common requirement and many methods can be used. Any mathematical operator except the concatenation/addition operator will force type-conversion to number.  
 
=== Converting to Object ===
Property access operation on string, number, and boolean primitives results in the creation of a temporary object.
<source language="javascript">
true.toString(); // Boolean Object.
1.2.valueOf(); // Number object.
" foo ".trim(); // String Object.
 
// null.toString(); // TypeError
// undefined.toString(); // TypeError
</source>

Revision as of 21:10, 5 January 2014

There are five primitive types in JavaScript: Null, Undefined, Boolean, String, Number.

Various operations in JavaScript require conversion to and from primitive values.

Converting to Boolean

When evaluating the expression of an if statement the Javascript interpreter will type-convert the result of that expression to boolean in order to make its decision. Also various operators internally type-convert their operands to boolean in order to determine what action to take. These include the logical operators like AND (&&), OR (||) and NOT (!). The NOT operator type-converts its operand to boolean and if that value is boolean true it returns false and if false it returns true. As the result of a NOT operation is a boolean value that is the inverse of the type-converted true-ness of its operand, two NOT operations together will return a boolean value that is equivalent to the result of type-converting the operand to boolean.

Converting to String

When either operand is a string, the concatenation is performed.

Whenever the + operator is used, the operands must be converted into primitive values. First, the interpreter calls the object's valueOf to get a primitive value. If the result is primitive, then that value is used. Example:

<source language="javascript"> var o = { valueOf : function() { return 1; } o + 1; // 2. </source> Otherwise, the object's toString is called. <source language="javascript"> var o = { toString : function() { return "1"; } } o + 1; // "11". </source>

Converting to Number

Converting strings is a very common requirement and many methods can be used. Any mathematical operator except the concatenation/addition operator will force type-conversion to number.

Converting to Object

Property access operation on string, number, and boolean primitives results in the creation of a temporary object. <source language="javascript"> true.toString(); // Boolean Object. 1.2.valueOf(); // Number object. " foo ".trim(); // String Object.

// null.toString(); // TypeError // undefined.toString(); // TypeError </source>