JavaScript/Notes/TypeConversion

From Noisebridge
< JavaScript(Difference between revisions)
Jump to: navigation, search
(The typeof Operator)
Line 87: Line 87:
 
</source>  
 
</source>  
  
=== Primitive to Object ===  
+
=== Primitive to Object with Property Accessors <code>.</code> and <code>[]</code> ===  
 
Property access operation on string, number, and boolean primitives results in the creation of a '''temporary'' object, for the algorithm.
 
Property access operation on string, number, and boolean primitives results in the creation of a '''temporary'' object, for the algorithm.
 
<source lang="javascript">
 
<source lang="javascript">
Line 99: Line 99:
 
</source>
 
</source>
  
 +
See also: http://dhtmlkitchen.com/how-property-access-works/
 +
 +
=== Object Creation ===
 
<source lang="javascript">
 
<source lang="javascript">
 
// You'll never need these.
 
// You'll never need these.
Line 104: Line 107:
 
new Object(true); // Results a Boolean object.
 
new Object(true); // Results a Boolean object.
 
new Object("");  // Results a String object.
 
new Object("");  // Results a String object.
 +
JSON.parse("{}"); // Results an Object object.
 
</source>
 
</source>
  
Line 115: Line 119:
 
<tr>
 
<tr>
 
<th>Type <th>Result
 
<th>Type <th>Result
<tr><td>Undefined <td>"undefined"
+
<tr style="background: #eee"><td>Undefined <td>"undefined"
 
<tr><td>Null <td>"object"
 
<tr><td>Null <td>"object"
<tr><td>Boolean <td>"boolean"
+
<tr style="background: #eee"><td>Boolean <td>"boolean"
 
<tr><td>Number <td>"number"
 
<tr><td>Number <td>"number"
<tr><td>String <td>"string"
+
<tr style="background: #eee"><td>String <td>"string"
 
<tr><td>Object (native or host and doesn't implement <nowiki>[[Call]]</nowiki>) <td>"object"
 
<tr><td>Object (native or host and doesn't implement <nowiki>[[Call]]</nowiki>) <td>"object"
<tr><td>Object (native or host and implements <nowiki>[[Call]]</nowiki>) <td>"function"
+
<tr style="background: #eee"><td>Object (native or host and implements <nowiki>[[Call]]</nowiki>) <td>"function"
 
</table>
 
</table>
 
See also: http://dhtmlkitchen.com/how-property-access-works/
 

Revision as of 12:26, 6 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.

Contents

Converting to Boolean

When evaluating any expression that requires a boolean value, the expression must be converted into a boolean using the internal [[ToBoolean]].

For example:

var n = 0;
if(n) { // false
}
 
var t = !""; // true. Empty string is falsy.
var f = !"f"; // false. Non-empty strings are not falsy.
Boolean(""); // false.

All numbers boolean-convert to true except for the following: +/-0 and NaN

Boolean operators use type-conversion for the evaluation of their left hand side operands.

1 && 0;            // 0.
"" || 0;           // 0.
null || undefined; // undefined.
undefined || 1;    // 1.
NaN || 0;          // 0;

All falsy values:

false
""
null
undefined
0
NaN

All other primitive values and all objects are truthy.

Converting to String

With the + operator, when either operand is a string, concatenation is performed.

All native objects have a toString method. Number.prototype.toString(base) is special in that it takes a base parameter.

15..toString(16)
String(15); // Calls ToPrimitive(input argument, hint String).

Object to Primitive

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 a primitive value, then that value is used. Example:

var o = { 
  valueOf : function() { return 1; } 
};
o + 1; // 2.

Otherwise, if o.valueOf results in an object —and Object.prototype.valueOf does — the object's toString is called.

var o = { toString : function() { return "1"; } };
o + 1; // "11".

Example: toString, valueOf, and concatenation

Converting to Number

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

parseInt(s, radix)

To force use of a particular base, use the radix parameter:

parseInt("09", base) // base from 2 to 36.

If radix is omitted, the base is determined by the contents of the string. Any string beginning with 0x or 0X represents a hexadecimal number. A string beginning with a leading 0 may, in older implementations, be parsed as octal (as if raxix were 8), in ECMA-262 Ed 3 (octal digits are 0-7). If string 09 is converted to 0.

var t = "0xf";
Number(t); // 15
+t;        // 15

Primitive to Object with Property Accessors . and []

Property access operation on string, number, and boolean primitives results in the creation of a 'temporary object, for the algorithm.

// Primitive to Object conversion.
true.toString(); // creates a temporary Boolean Object.
1.2.valueOf();   // creates a temporary Number object.
" foo ".trim();  // creates a temporary String Object.
 
// null.toString(); // TypeError
// undefined.toString(); // TypeError

See also: http://dhtmlkitchen.com/how-property-access-works/

Object Creation

// You'll never need these.
new Object(1);    // Results a Number object.
new Object(true); // Results a Boolean object.
new Object("");   // Results a String object.
JSON.parse("{}"); // Results an Object object.

Type Checking

The typeof Operator

typeof someval;
Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native or host and doesn't implement [[Call]]) "object"
Object (native or host and implements [[Call]]) "function"
Personal tools