# JavaScript String to Boolean Conversion Methods

> Convert a true or false string to a boolean in JavaScript using the strict equality operator, Boolean() constructor, or the double NOT operator

**URL:** https://sentry.io/answers/how-can-i-convert-a-string-to-a-boolean-in-javascript/

---

## The Problem

You have a string that you want to convert to a boolean. You may have a boolean value that gets converted to a string, for example when values are submitted via a form. How do you convert a "true" or "false" string to a boolean?

## The Solution

You can use an equality operator, `Boolean()` constructor, or the double NOT (`!!`) operator to convert a "true" or "false" string to a boolean.

### Using the Strict Equality Operator (`===`)

You can parse the string to a boolean using the [strict equality operator(`===`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality). You can check if the value is equal to "true":

```javascript
let trueStr = "true";
trueStr = (trueStr === "true");
console.log(trueStr, typeof trueStr); // true "boolean"
```

This would also work using the [equality operator (`==`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality), which will try to coerce the values being compared to the same type before comparing them. It's best to use the strict equality operator (`===`), as the type conversion of the equality operator (`==`) can lead to unexpected results:

```javascript
console.log("" === "0"); // false
console.log(0 == ""); // true
console.log(0 == "0"); // true
console.log(false == "false"); // false
console.log(false == "0"); // true
console.log(false == ""); // true
```

You can also use the `toLowerCase()` method to do a case-insensitive check for "true":

```javascript
let trueStr = "true";
trueStr = (trueStr.toLowerCase() === "true");
console.log(trueStr, typeof trueStr); // true "boolean"
```

The problem with the above code is that it will throw an error if the `trueStr` variable is `null` or `undefined`:

```
Cannot read properties of undefined (reading 'toLowerCase')
```

There are two fixes for this. You can use the [`typeOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) operator to check that the value is a string before lower casing the value:

```javascript
let trueStr = null;
trueStr = (typeof trueStr === "string" && trueStr.toLowerCase() === "true");
console.log(trueStr, typeof trueStr); // false "boolean"
```

You can also use an if statement to first check if the `trueStr` variable is defined:

```javascript
let trueStr = null;
if (trueStr) {
  trueStr = (trueStr.toLowerCase() === "true");
}
console.log(trueStr, typeof trueStr); // null "object"
```

### Using the `Boolean()` Constructor or the Double NOT (`!!`) Operator

You can use the [`Boolean()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean) constructor to create a boolean object and then use its `valueOf` property to get the primitive boolean value. It takes in a value to convert to a boolean. If the value is [`falsy`](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) it will return `false`. If the value is [`truthy`](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), it will return `true`. All values are truthy except: `false`, `0`, `-0`, `0n`,  `""`, `null`, `undefined`, and `NaN`.

The [double NOT (`!!`) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!) coerces a value to a primitive boolean value, like the `Boolean()` constructor:

```javascript
const str1 = "true";
const str2 = "false";
const str3 = "";
const str4 = " ";

console.log(!!str1); // true
console.log(!!str2); // true
console.log(!!str3); // false
console.log(!!str4); // true

console.log(Boolean(str1).valueOf()); // true
console.log(Boolean(str2).valueOf()); // true
console.log(Boolean(str3).valueOf()); // false
console.log(Boolean(str4).valueOf()); // true
```

These methods are not ideal as "false" returns `true` because the only falsy string is an empty string.

---

*Source: [sentry.io/answers/how-can-i-convert-a-string-to-a-boolean-in-javascript/](https://sentry.io/answers/how-can-i-convert-a-string-to-a-boolean-in-javascript/)*
