JSFiddle - React, Tailwind, and code Playground
HTML
<div id="output"></div>
CSS
body {font: 0.8em Arial, Verdana, sans-serif;}
p {line-height: 0.5em;}
span {font-style: normal; font-weight: bold;}
JavaScript
$(document).ready(function () {
(function () {
'use strict';
var log = function (msg) {
var p = $('<p />').html(msg);
$('#output').append(p);
},
truthitizeSwitch = function (val) {
switch (true) { // use a boolean to force case statement to evaluate conditionals
case (val ? true : false): // force a truthy/falsy evaluation of val using parentheses and the ternary operator
log('<span>' + val + '</span> evaluates as <span>truthy</span> in the switch statement.');
break;
default:
log('<span>' + val + '</span> evaluates as <span>falsy</span> in the switch statement.');
break;
}
return !!val; // use !! to return a coerced boolean
},
truthitizeIf = function (val) {
if (val) { // if statement naturally forces a truthy/falsy evaluation
log('<span>' + val + '</span> evaluates as <span>truthy</span> in the if statement.');
} else {
log('<span>' + val + '</span> evaluates as <span>falsy</span> in the if statement.');
}
return !!val; // use !! to return a coerced boolean
},
tests = [
undefined, // falsey: undefined
null, // falsey: null
parseInt('NaNificate this string'), // falsey: NaN
'', // falsey: empty string
0, // falsey: zero
false, // falsey: boolean false
{}, // truthy: empty object
{"foo": "bar"}, // truthy:...