Danny Guo | 郭亚东

How to Convert Any JavaScript Value to a Boolean

 ·  252 words  ·  ~2 minutes to read

The best way to convert a JavaScript value to a boolean is to use the Boolean() function. It returns false for any falsy value and true otherwise (any value that is not falsy is truthy). You can try out the examples in this post with this Replit.

// Falsy values
const a = Boolean(null);
const b = Boolean("");
const c = Boolean(0);
const d = Boolean(false);

// Truthy values
const e = Boolean({});
const f = Boolean("foobar");
const g = Boolean(1);
const h = Boolean(true);

// The values are boolean primitives (`false` and `true`)
console.log({a, b, c, d, e, f, g, h});

Not Operator

An alternative is to use the not operator (!) twice. The first one returns a primitive boolean value, and then the second one flips the value to what you actually want.

const fooString = "";
const fooBoolean = !!fooString;
// The value is `false`
console.log({fooBoolean});

This method takes fewer characters to write, but I prefer to use the Boolean() method because I think it’s clearer and more readable. We should generally optimize for reading code over writing code.

Boolean Constructor

Make sure you don’t use Boolean as a constructor (using new) because that creates an object. You would have to use .valueOf() to get a primitive value.

// bar is an object
const bar = new Boolean("");

if (bar) {
    // So this would run because an object is truthy
    console.log("in bar branch");
}

const barValue = bar.valueOf();
if (barValue) {
    // But this wouldn't run because barValue is `false`
    console.log("in barValue branch");
}

console.log({bar, barValue});

← My Credit and Debit Card Collection Start With a Minimum Viable System →

Follow me on Twitter or Mastodon or subscribe to my newsletter or RSS feed for future posts.

Found an error or typo? Feel free to open a pull request on GitHub.