JavaScript/Notes/TypeConversion

From Noisebridge
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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 using the internal [[ToBoolean]].

For example: <source lang="javascript"> var n = 0; if(n) { // false }

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

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. <source lang="javascript"> 1 && 0; // 0. "" || 0; // 0. null || undefined; // undefined. undefined || 1; // 1. NaN || 0; // 0; </source>

All falsy values: <source lang="javascript"> false "" null undefined 0 NaN </source>

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.

<source lang="javascript"> 15..toString(16) </source>

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:

<source lang="javascript"> var o = {

 valueOf : function() { return 1; } 

}; o + 1; // 2. </source>

Otherwise, if o.valueOf results in an object —and Object.prototype.valueOf does — 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 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) (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.

Primitive to Object

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

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

Type Checking

The typeof Operator

<source lang="javascript"> typeof someval; </source>

Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native and doesn't implement [[Call]]) "object"
Object (native or host and implements [[Call]]) "function"
Object (host) Implementation-dependent

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