Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I convert a string to boolean in JavaScript?
This tutorial teaches us to convert a string to a boolean in JavaScript. When developing applications, you might store boolean values in databases or local storage as strings. Later, you need to convert these string values back to boolean to perform specific operations.
We'll explore three effective methods to convert strings to boolean values in JavaScript.
- Using the Comparison (==) and Ternary operator (? :)
- Using the Boolean constructor
- Using the Double Not (!!) Operator
Using the Comparison (==) and Ternary operator (? :)
The most straightforward approach combines the comparison ('==') operator with the ternary (? :) operator to explicitly check string values.
Syntax
string_var == 'true' ? true : false; string_var == 'false' ? false : true; string_var.toLowerCase() == 'true' ? true : false; // case-insensitive approach
Parameters
- string_var ? Any string that you want to convert into boolean.
Example
In this example, we convert various string values to boolean using comparison and ternary operators:
<html>
<head>
<title>Convert String to Boolean</title>
</head>
<body>
<h2>Converting String to Boolean in JavaScript</h2>
<h4>String after converting to boolean</h4>
<p id="contentDiv"></p>
<script>
// function to convert string to boolean
function stringToBoolean() {
let string1 = "True";
let string2 = "false";
let string3 = "TRUE";
let bool1 = string1.toLowerCase() == 'true' ? true : false;
let bool2 = string2.toLowerCase() == 'true' ? true : false;
let bool3 = string3.toLowerCase() == 'true' ? true : false;
document.getElementById('contentDiv').innerHTML =
"<p>'" + string1 + "' converts to: " + bool1 + " (type: " + typeof bool1 + ")</p>" +
"<p>'" + string2 + "' converts to: " + bool2 + " (type: " + typeof bool2 + ")</p>" +
"<p>'" + string3 + "' converts to: " + bool3 + " (type: " + typeof bool3 + ")</p>";
}
stringToBoolean();
</script>
</body>
</html>
Using the Boolean Constructor
The Boolean constructor takes any value as an argument and returns a boolean based on its "truthiness". However, it has a specific behavior: it returns true for all non-empty strings, regardless of their content.
The six falsy values in JavaScript are:
- false
- 0
- NaN
- "" (empty string)
- null
- undefined
Syntax
let bool = Boolean(string);
Parameters
- string ? Any string value to convert into boolean.
Example
This example demonstrates how the Boolean constructor works with different string values:
<html>
<head>
<title>Convert String to Boolean</title>
</head>
<body>
<h2>Converting String to Boolean using Boolean Constructor</h2>
<h4>String after converting to boolean</h4>
<p id="contentDiv"></p>
<script>
function stringToBoolean() {
let bool1 = Boolean("false"); // "false" string
let bool2 = Boolean(""); // empty string
let bool3 = Boolean("true"); // "true" string
let bool4 = Boolean("0"); // "0" string
document.getElementById('contentDiv').innerHTML =
"<p>Boolean('false') = " + bool1 + "</p>" +
"<p>Boolean('') = " + bool2 + "</p>" +
"<p>Boolean('true') = " + bool3 + "</p>" +
"<p>Boolean('0') = " + bool4 + "</p>" +
"<p>Data type: " + typeof bool1 + "</p>";
}
stringToBoolean();
</script>
</body>
</html>
Important: Note the difference between these two:
Boolean(false); // false (boolean value)
Boolean("false"); // true (non-empty string)
Using the Double Not (!!) Operator
The Not (!) operator converts any value to boolean and inverts it. The double Not (!!) operator applies this conversion twice, giving you the actual boolean representation of the value.
Syntax
let bool = !!string_val;
Parameters
- string_val ? Any string to convert into boolean.
Example
<html>
<head>
<title>Convert String to Boolean</title>
</head>
<body>
<h2>Converting String to Boolean using Double Not Operator</h2>
<h4>String after converting to boolean</h4>
<p id="contentDiv"></p>
<script>
function stringToBoolean() {
let string1 = "tutorialsPoint";
let string2 = "";
let singleNot1 = !string1; // single Not operator
let doubleNot1 = !!string1; // double Not operator
let singleNot2 = !string2; // single Not on empty string
let doubleNot2 = !!string2; // double Not on empty string
document.getElementById('contentDiv').innerHTML =
"<p>!'" + string1 + "' = " + singleNot1 + "</p>" +
"<p>!!'" + string1 + "' = " + doubleNot1 + "</p>" +
"<p>!'' = " + singleNot2 + "</p>" +
"<p>!!'' = " + doubleNot2 + "</p>" +
"<p>Data type of doubleNot1: " + typeof doubleNot1 + "</p>";
}
stringToBoolean();
</script>
</body>
</html>
Comparison of Methods
| Method | Best for | Handles "false" string | Case sensitive |
|---|---|---|---|
| Comparison + Ternary | Explicit string-to-boolean conversion | Yes | Can be made case-insensitive |
| Boolean constructor | Truthiness conversion | No ("false" ? true) | N/A |
| Double Not (!!) | Quick truthiness conversion | No ("false" ? true) | N/A |
Conclusion
For explicit string-to-boolean conversion where "false" should become false, use the comparison and ternary operator approach. For general truthiness conversion, the Boolean constructor or double Not operator work well, but remember they treat any non-empty string as true.
