How to Convert String to Boolean in JavaScript

How to Convert String to Boolean in JavaScript? Let’s say we want to do a specific operation on our website or application depending on some boolean values saved as strings in our database. In such a situation, before employing those texts in logical operations, we must turn them into boolean values.

This article will examine various JavaScript methods for converting string values to boolean (true or false) values.

Convert String to Boolean in JavaScript Using Identity Operator (===)

Only when and only if both values being compared are of the same type and have the same value does the identity operator, often referred to as a stringent equality operator, return true. In other words, it checks to see if the values on the left and right are equal; if they are, it returns true or false.

Essentially, we’ll contrast our string with the word “true.” So, only if our text genuinely contains the word “true” will the output be a boolean true. The code will return a false boolean value for any other string:

let mystr = "true";
let boolResult = mystr === "true";
console.log("Converted value: " + boolResult);

if (boolResult) {
  console.log(true);
} else {
  console.log(false);
}

Output:-

Converted value: true
true

Additionally, to ensure that the letter case won’t result in any incorrect outputs, we can convert a string to lowercase first:-

let mystr = "True";
let boolResult = mystr.toLowerCase() === "true";
console.log("Converted value: " + boolResult);

if (mystr) {
  console.log(true);
} else {
  console.log(false);
}

Output:-

Converted value: true
true

As we previously mentioned, the prior code will output false if the value of our string is not equal to “true”:-

let myString = "Hello";
let boolOutput = myString === "true";
console.log(boolOutput);

Output:-

false

let myString = "Hello";
let boolOutput = myString.toLowerCase() === "true";
console.log(boolOutput);

Output:-

false

Convert String to Boolean in JavaScript using Equality and Ternary Operator

The ternary operator can add a little spice to the situation along with the equality operator. All we need to do is determine whether our string matches “true” and then return true or false depending on whether it does or not:-

let mystr = "Hello";
let boolValue = mystr.toLowerCase() == "true" ? true : false;
console.log(boolValue);

Output:-

false

This article showed how to convert a string into a boolean in JavaScript. The simplest method to do this is to compare our string value to “true” using the strict equality operator; if the string is (strictly) equal to “true,” the result will be a boolean true. As an alternative, you can accomplish the same thing by combining the loose equality operator and the ternary operator. Additionally, pattern matching is a reliable strategy.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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