How To Convert Truthy Or Falsy Values To Boolean In JavaScript?

Convert Truthy or Falsy values to Boolean in JavaScript

Hello guys! Today we will guide you to convert truthy or falsy values to Boolean in JavaScript. We will provide you with 3 methods so you can choose which works best for you, that is using if else, using boolean() and using !! operator.

Convert truthy or falsy values to Boolean in JavaScript

We are going to provide you with two different approaches. Each way is relatively easy so you can use whatever you like.

Using if else

The if else statement is the most fundamental thing in a programming language. It will check the condition in the if guard and then see if the expressions return true.

For example:

// Define a falsy value
let falsy = undefined;
 
// Convert Falsy values to Boolean
if (falsy) {
  console.log(true);
} else {
  console.log(false);
}

Output:

false

The example above indicates that we have declared the falsy value in this case equals undefined. Then we use the if statement to convert it to a boolean value. If you don’t want to print out the result, you can assign the boolean value to the variable instead:

 // Define a falsy value
let falsy = undefined;
 
// Define result variable
let result;
 
// Convert Falsy values to Boolean
if (falsy) result = true;
else result = false;

console.log(result);

Output

false

Using Boolean()

Syntax:

Boolean(value)

Parameter:

  • value: The truthy or falsy value to be converted to.

The Boolean() constructor can be used to convert a truthy value or falsy value to boolean value, all you have to do is passing that truth or falsy value into its parameter when calling Boolean() function.

// Define a falsy and truth value
let falsy = null;
let truthy = 1;
 
// Convert Truthy or Falsy values to Boolean
let result = Boolean(falsy);
let result2 = Boolean(truthy);

console.log(result);
console.log(result2);

Output:

false
true

In JavaScript, a falsy value is equal to one of the following values: 0 or -0 (in numeric type), an empty string (‘’), undefined or null, and of course, false or NaN.

Using !! operator

Syntax:

!!value

Parameter:

  • value: The truthy or falsy value to be converted to.

This operator is actually based on two unary operators NOT, this NOT operator (!) will return true with falsy values and return false with truthy ones. Therefore, if you want to convert them to their actual boolean, you must use two operators at the same time.

For example:

// Define a falsy and truth value
let falsy = -0;
let truthy = '0';
 
// Convert Truthy or Falsy values to Boolean
let result = !!falsy;
let result2 = !!truthy;
 
console.log(result);
console.log(result2);

Output:

false
true

As you can see, this solution also works well the same as two other ones. Here is another example of working with ! operator:

// Define a falsy and truth value
let falsy = !true;
let truthy = 'false';
 
// Convert Truthy or Falsy values to Boolean
let result = !!falsy;
let result2 = !!truthy;
 
console.log(result);
console.log(result2);

Output:

false
true

Remember that if the value you provide is not a falsy value, such as a zero string (‘0’ or ‘-0’) or an empty object, etc., it will be treated as a truthy value, so check out what the falsy values are as we have explained at the end of in the previous solution.

Summary

We have learned to convert truthy or falsy values to Boolean in JavaScript. You can quickly achieve the goal using the first solution – using if else, we have introduced. We also have a lot of tutorials about JavaScript, which you can find here.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *