I am trying to determine if an object property value is "truthy" via a switch statement.
Using this sample block:
var test = {
foo: "bar"
}
switch(true) {
case test.foo:
console.log("success in switch");
break
default:
console.log("no success in switch");
break
}
if (test.foo) {
console.log("success in if");
} else {
console.log("no success in if");
}
ends up logging:
"no success in switch"
"success in if"
What is the proper way to do this?
To check if a value is truthy, pass the value to an if statement, e.g. if (myValue) . If the value is truthy, it gets coerced to true and the if block runs. Copied! The if statement checks if the value stored in the variable is truthy.
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN . JavaScript uses type coercion in Boolean contexts.
Checking for falsy values on variables It is possible to check for a falsy value in a variable with a simple conditional: if (! variable) { // When the variable has a falsy value the condition is true. }
You can do this :
case !!test.foo:
This would force a conversion to boolean.
In addition to using !!
to coerce a boolean, you can do this with the switch
statement to evaluate truthy/falsy:
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
console.log(val + ' evaluates as truthy in the switch statement.');
break;
default:
console.log(val + ' evaluates as falsy in the switch statement.');
break;
}
Here's a set of functions and tests so you can see for your self:
(function () {
'use strict';
var 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
console.log(val + ' evaluates as truthy in the switch statement.');
break;
default:
console.log(val + ' evaluates as falsy 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
console.log(val + ' evaluates as truthy in the if statement.');
} else {
console.log(val + ' evaluates as falsy 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: non-empty object
-1, // truthy: negative non-zero number
'asdf', // truthy: non-empty string
1, // truthy: positive non-zero number
true // truthy: boolean true
],
i;
for (i = 0; i < tests.length; i += 1) {
truthitizeSwitch(tests[i]);
truthitizeIf(tests[i]);
}
}());
And, of course :), the obligatory jsFiddle: http://jsfiddle.net/AE8MU/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With