Truthiness in JavaScript

Chris Hosler
Jan 15 · 3 min read
Image for post
Image for post

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!

The Startup

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store