Truthiness in JavaScript

I’ve spend a couple of articles talking about falsy values in JavaScript (if you’re interested…NaN and Null and Undefined), and I wanted to turn things around and talk about truthy values in JavaScript.
To recap my previous articles, in JavaScript there are six values that are considered to be falsy, and they are false
, 0
, ‘’
, null
, undefined
, and NaN
. Now if you have, basically, any other value than one of these six, then there’s a good chance that value will be truthy. Now this does not mean that the value itself is equal to true
but that when the value is evaluated in boolean terms it will be coerced into true
. This can be demonstrated in code as…
function isTruthy(value) {
if (value) {
console.log("I'm Truthty!");
} else {
console.log("I'm Falsy!!");
}
}
Here we create a simple function that when supplied with a value
will perform a boolean check to see if the value
is true
or false
, given a true
value it will tell us that it is truthy and given a false
value it will tell us that it is falsy. To test it, let’s give it a value that we know is falsy, such as false
…
isTruthy(false); // Outputs "I'm Falsy!!"
…and if we give it a value that should be truthy, let’s use true…
isTruthy(true); // Outputs "I'm Truthy!"
So using our isTruthy function we can go ahead and see if a value is truthy or not…
isTruthy(NaN); // Outputs "I'm Falsy!!"isTruthy(undefined); // Outputs "I'm Falsy!!"isTruthy(null); // Outputs "I'm Falsy!!"isTruthy(0); // Outputs "I'm Falsy!!"isTruthy(''); // Outputs "I'm Falsy!!"isTruthy("false"); // Outputs "I'm Truthy!"isTruthy(10); // Outputs "I'm Truthy!"isTruthy(new Date()); // Outputs "I'm Truthy!"isTruthy([]); // Outputs "I'm Truthy!"isTruthy({}); // Outputs "I'm Truthy!"isTruthy(new Boolean(false)); // Outputs "I'm Truthy!"isTruthy(new String('')); // Outputs "I'm Truthy!"isTruthy(new Number(NaN)); // Outputs "I'm Truthy!"
Do the last three surprise you at all? After all I told you that false
, ‘‘
, and NaN
were all falsy values. Well, if we take a look at new Boolean(false)
, and if we do typeof(new Boolean(false))
we will get object
, and an object
is always true in JavaScript, just like {}
is truthy, even though it’s empty.
Well that pretty much wraps it up, the big takeaway here is the six falsy values: 0
, ‘’
, NaN
, undefined
, null
, and false
and as long as you’re not dealing with one of those six then you’re probable dealing with a truthy value. This is useful when it comes to conditional statements, because if you’re conditional isn’t doing exactly what you expected it to, make sure you’re not giving it a falsy value and expecting it to evaluate to true, for instance…
function iWantThisToRun(value) {
value ? console.log("Running!!") : console.log("Nope!!!");
}function doingSomething(value) {
let newValue = value - 10;
return iWantThisToRun(newValue);
}doingSomething(10) // Outputs "Nope!!!"
Here, we end up feeding our iWantThisToRun
function a value of 0
, and 0
is falsy, so our true
conditional will never happen.
Well, I hope that you found this useful, and thanks for reading!