JavaScript

How to Convert a String to Boolean in JavaScript

Sometimes boolean values are stored in databases as strings, and programmers may use those values to perform specific actions on websites or applications. In that situation, before using those strings in logical operations, they must be converted into boolean values.

This tutorial will demonstrate the methods for converting string to boolean using JavaScript.

How to Convert/Transform a String Into Boolean Using JavaScript?

Use the following methods for converting a string into a boolean using JavaScript:

  • Strict equality operator (===)
  • Double not (!!) operator
  • Boolean Object

Let’s see how these methods will work.

Method 1: Convert a String to Boolean Using Strict Equality (===) Operator

The “Strict equality(===) Operator or the “identity” operator is utilized for converting a string to a boolean value. It verifies whether the left-hand side value becomes equal to the right-hand side value. If yes! it returns “true” else, it returns “false”.

Syntax
The syntax for the strict equality operator is as follows.:

a === b

Return value
Its outputs “true” if the compared values consist of the same value and type.

Example 1:
Create a variable named “string” that stores a boolean value “true” as a string:

var string = 'true';

Compare the string to the string “true” using the “Strict equality(===) Operator. Only if the string is “true”, the output will be allocated a boolean value “true”:

var result = string === 'true';

Print the result on the console using the “console.log()” method:

console.log(result);

Output

The output displays “true”, as the strict equality returns true when both operands are equal in terms of the type and value.

Example 2:
In variable “string”, store boolean value “false”:

var string = 'false';

Compare the string “false” with the string “true”:

var result = string === 'true';

Output

The output shows “false” because the strict equality operator returns true if the string is actually “true”.

Method 2: Convert a String to Boolean Using Double NOT (!!) Operator

To convert string to boolean, there is another method in JavaScript, known as a double exclamation (!!) that is a double NOT (!!) operator. It returns a boolean value by reversing the result of a single NOT operator.

Syntax
The syntax for the double NOT (!!) operator is as follows:

!!string

In the above syntax:

  • The first (!) operator changes it to an inverted boolean value. The second (!) operator inverts the inverted boolean value. In other words, it is now the actual Boolean value of the object.

Example 1:
Create a variable “string” and store a boolean value “true” as a string in it:

var string = 'true'

Use the double NOT (!!) operator with string to convert into a boolean value:

console.log(!!string);

Output

Output displays “true”, as in (!!) operator, first (!) converts “true” into “false”, then the second (!) again converts it into “true”.

Method 3: Convert a String to Boolean Using Boolean Object

For converting the string to a boolean, use the JavaScript built-in “Boolean” object. It is a wrapper object for boolean values.

Syntax
The syntax for converting string to boolean with the help of a Boolean object is as follows:

Boolean(string)
  • It takes a string as an argument and returns a boolean value.
  • It returns “true” if the passed string is not empty.
  • For an empty string, it returns “false”.

Example 1:
Create a variable “string” and store a boolean value “true” as a string in it:

var string = 'true'

Call the Boolean wrapper by passing the string:

Boolean(string);

Output

The output returns a boolean value “true”, as the passed string is not empty.

Example 2:
Store the boolean value “false” in a variable “string”:

var string = 'false'

Invoke the Boolean wrapper by passing the string:

Boolean(string);

The corresponding output will be:

Conclusion

To convert a string to a boolean, use the “Strict equality” operator (===) that compares the specified string to the string “true” and it returns a boolean value “true” if the compared values are of the same type and value. The “Double not” (!!) operator returns a boolean value by reversing the result of a single NOT operator, or JavaScript “Boolean” Object that returns a boolean value “true” if the passed string is not an empty string else return “false”. This tutorial demonstrates the methods for converting string to boolean using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.