JavaScript/Notes/TypeConversion: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
Garrett (talk | contribs)
Garrett (talk | contribs)
No edit summary
Line 14: Line 14:
var f = !"f"; // Non-empty strings are not falsy.
var f = !"f"; // Non-empty strings are not falsy.
</source>
</source>
Numbers:
Numbers:
<table>
<table>
Line 36: Line 37:
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:'''
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">
<source lang="javascript">
var o = { valueOf : function() { return 1; }  
var o = { valueOf : function() { return 1; }  
o + 1; // 2.
o + 1; // 2.
</source>
</source>
Otherwise, the object's toString is called.  
Otherwise, the object's toString is called.  
<source language="javascript">
<source lang="javascript">
var o = { toString : function() { return "1"; } }
var o = { toString : function() { return "1"; } }
o + 1; // "11".
o + 1; // "11".
Line 51: Line 53:
=== Converting to Object ===  
=== Converting to Object ===  
Property access operation on string, number, and boolean primitives results in the creation of a temporary object.
Property access operation on string, number, and boolean primitives results in the creation of a temporary object.
<source language="javascript">
<source lang="javascript">
true.toString(); // Boolean Object.
true.toString(); // Boolean Object.
1.2.valueOf(); // Number object.
1.2.valueOf(); // Number object.

Revision as of 21:22, 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 any expression that requires a boolean value, the expression must be converted into a boolean.

For example: <source language="javascript"> if(0) { // ToBoolean }

var t = !""; // Empty string is falsy. var f = !"f"; // Non-empty strings are not falsy. </source>

Numbers:

Falsy Numbers
-0 +0 NaN

All other numbers boolean convert to true.

Boolean operators type-convert their operands 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 lang="javascript"> var o = { valueOf : function() { return 1; } o + 1; // 2. </source>

Otherwise, the object's toString is called. <source lang="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 lang="javascript"> true.toString(); // Boolean Object. 1.2.valueOf(); // Number object. " foo ".trim(); // String Object.

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