How to convert String to Boolean JavaScript? [SOLVED]


JavaScript, How To

Introduction

One of the most basic data types in JavaScript is the boolean data type. A boolean data type can have only two values, true or false. In some programming languages, such as C, C++, and Java, the data type is represented by the keywords true and false. In JavaScript, the boolean data type is represented by the keywords true and false as well. In this article, we will explain how to convert string to Boolean in JavaScript.

 

String conversion and Type Coercion

Before we go on, it is important to know about type coercion in JavaScript. Type coercion is the process of converting one data type to another. In JavaScript, this is often done automatically by the interpreter when two values are combined, such as when using the + operator to add numbers, or the + operator to concatenate strings. This is the basis for how a lot of type conversion is done.

Furthermore, Boolean coercion is the process of converting a value (e.g. string) to a Boolean data type. This can be done using the Boolean() function or by using the ! (not) operator, which is our goal in this article. For us to convert a string to Boolean, we need to know that it is possible to coerce the string value to a Boolean value (which we have established is possible).

 

Use the Boolean method to convert string to Boolean

The Boolean() function is used to convert a string to a Boolean data type. All objects become true when we pass it through the Boolean() method. So, the function returns the value true as long as a character is present between the single or double quotes. Only an empty string will return false.

let myStr = "";
let otherStr = "wow";
let anotherStr = "false";

console.log(Boolean(myStr));
console.log(Boolean(otherStr));
console.log(Boolean(anotherStr));

Output

false
true
true

 

Use the ! operator to convert string to Boolean

Another operator we can use is the ! operator. The ! operator negates any Boolean value, so if applied on true, it becomes false and vice versa. However, if you apply it on a string or other data types, it coerces that data type to a Boolean value (based on what it would give via the Boolean method).

For example, if we apply the ! operator on the myStr and otherStr binding, it would convert the string the bindings hold to Boolean, then negate the Boolean value.

let myStr = "";
let otherStr = "wow";

console.log(!myStr);
console.log(!otherStr);

Output

true
false

The myStr binding which holds an empty string is converted to false, then negated, which resulted to true, and the otherStr binding holds "wow" which is converted to true, then negated to false.

However, if we want to retain its actual direct Boolean value, we need to apply the ! operator twice.

console.log(!!myStr);
console.log(!!otherStr);

Output

false
true

 

Summary

To convert string to Boolean in JavaScript, we have two approaches. One is the use of the Boolean method which directly converts data types (including strings) to corresponding Boolean values. And two, the use of the ! (not) operator.

 

References

Boolean - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment