## Javascript primitive conversion quick-reference

Javascript has three primitive types: number, string and boolean. You can quickly coerce values between the primitive types using some simple expressions.

There are a few different coersion expressions, depending on how you want to handle some of the corner cases.  I’ve automatically generated a list below:

Conversion: To Number To Number To Number To String To Boolean To Boolean
Expression: +x (+x)||0 +(x||0) “”+x !!x !!+x
null 0 0 0 “null” false false
(void 0) NaN 0 0 “undefined” false false
NaN NaN 0 0 “NaN” false false
Infinity Infinity Infinity Infinity “Infinity” true true
-Infinity -Infinity -Infinity -Infinity “-Infinity” true true
0 0 0 0 “0″ false false
“0″ 0 0 0 “0″ true false
1 1 1 1 “1″ true true
“1″ 1 1 1 “1″ true true
2 2 2 2 “2″ true true
“2″ 2 2 2 “2″ true true
[] 0 0 0 “” true false
({}) NaN 0 NaN “[object Object]“ true false
true 1 1 1 “true” true true
“true” NaN 0 NaN “true” true false
false 0 0 0 “false” false false
“false” NaN 0 NaN “false” true false
“” 0 0 0 “” false false
“null” NaN 0 NaN “null” true false

The above table was generated with this code (note: uses some Firefox-specific code).

```<table id="results" style="border-collapse: collapse; border: 1px solid black;">
<th>Conversion:</th>
</tr>
<th>Expression:</th>
</tr>
</table>

<script>
function styleCell(cell) {
cell.style.border = '1px solid black';
return cell;
}

values = [
null, undefined, NaN, +Infinity, -Infinity, 0, "0", 1, "1", 2, "2",
[], {}, true, "true", false, "false", "", "null"
]

coersions = [
["To Number", "+x"],
["To Number", "(+x)||0"],
["To Number", "+(x||0)"],
["To String", "\"\"+x"],
["To Boolean", "!!x"],
["To Boolean", "!!+x"]
]

var results = document.getElementById('results');

for (var i = 0; i < coersions.length; i++) {
th.textContent = coersions[i][0]
th.textContent = coersions[i][1]
}

for (var i = 0; i < values.length; i++) {
var tr = results.appendChild(document.createElement('tr'));