A boolean is a primitive value that represents either true or false. In Boolean contexts, JavaScript utilizes type casting to convert values to true/false. There are implicit and explicit methods to convert values into their boolean counterparts.

This article provides an overview of truthy and falsy values and how to convert values into booleans in JavaScript.

JavaScript Truthy and Falsy Values Cheatsheet

Boolean(false);         // false
Boolean(undefined);     // false
Boolean(null);          // false
Boolean('');            // false
Boolean(NaN);           // false
Boolean(0);             // false
Boolean(-0);            // false
Boolean(0n);            // false

Boolean(true);          // true
Boolean('hi');          // true
Boolean(1);             // true
Boolean([]);            // true
Boolean([0]);           // true
Boolean([1]);           // true
Boolean({});            // true
Boolean({ a: 1 });      // true
Source: What are truthy and falsy values in JavaScript?

It's best to start by first understanding which values are interpreted as truthy or falsy by JavaScript. It's also important to understand implicit coercion compared to explicit coercion.

Implicit coercion is initiated by the JavaScript engine and happens automatically. Explicit coercion is performed by manually converting values, and JavaScript provides built in methods to handle this.

The !! Operator

!!value

You may already be familiar with ! as the logical NOT operator. When using two in succession (!!), the first ! coerces the value to a boolean and inverts it. For example !true would result in false. The second ! reverses the previous inverted value, resulting in the true boolean value.

This is generally a preferred method, as it has better performance. A potential con to this method is a loss in readability, mainly if other developers are unfamiliar with how this operator works.

const value = "truthy string"
!!value // true

Here is an example breaking this down into steps:

const value = "truthy string";

!value; // false
!!value; // true

Below is a list of example output with the !! operator.

// Falsy Values

!!'' // false
!!false // false
!!null // false
!!undefined // false
!!0 // false
!!NaN // false


// Truthy Values

!![] // true
!!"false" // true
!!true // true
!!1 // true
!!{} // true

The Boolean() Function

Boolean(value)

Boolean() is a global function that converts the value it's passed into a boolean.

You shouldn't use this with the new keyword (new Boolean) as this creates an instance of a Boolean that has a type of object. Below is an example of the correct use of this function.

const value = "truthy string"
Boolean(value) // true

TL;DR

There are two methods to cast a value to a boolean in JavaScript.

1. !!

!!value

2. Boolean()

Boolean(value)
const finalThoughts = "I really enjoyed writing this article. Thanks for reading!"

!!finalThoughts // true
Boolean(finalThoughts) // true