Javascript Truthiness Table
by akogch
HTML
<table id="output"></table>
CSS
td {
font-family: Verdana, sans-serif;
font-size:13px;
padding:5px 10px;
}
.truthy {
background-color:#cfc;
}
.falsy {
background-color:#fcc;
}
JavaScript
function checkTruthiness(raw, exp) {
row = $('<tr />').appendTo('#output');
if ( exp ) {
row.append('<td class="truthy">' + raw + '</td>');
} else {
row.append('<td class="falsy">' + raw + '</td>');
}
if ( !exp ) {
row.append('<td class="truthy"> !' + raw + '</td>');
} else {
row.append('<td class="falsy"> !' + raw + '</td>');
}
if ( exp == true ) {
row.append('<td class="truthy">' + raw + ' == true </td>');
} else {
row.append('<td class="falsy">' + raw + ' == true </td>');
}
if ( exp == false ) {
row.append('<td class="truthy">' + raw + ' == false </td>');
} else {
row.append('<td class="falsy">' + raw + ' == false </td>');
}
if ( exp === true ) {
row.append('<td class="truthy">' + raw + ' === true </td>');
} else {
row.append('<td class="falsy">' + raw + ' === true </td>');
}
if ( exp === false ) {
row.append('<td class="truthy">' + raw + ' === false </td>');
} else {
row.append('<td class="falsy">' + raw + ' === false </td>');
}
}
checkTruthiness('0', 0);
checkTruthiness('NaN', NaN);
checkTruthiness('""', "");
checkTruthiness('false', false);
checkTruthiness('true', true);
checkTruthiness('null', null);
checkTruthiness('undefined', undefined);
checkTruthiness('{}', {});
checkTruthiness('[]', []);
checkTruthiness('"0"', "0");
checkTruthiness('"false"', "false");
checkTruthiness('"true"', "true");