/    Sign up×
Bounties /Pin to ProfileBookmark

How do you convert a string to boolean in JavaScript?

+ 1,000
Copy linkTweet thisAlerts:
Dec 06.2022

If a form field is expected to contain a boolean value (e.g., “true” or “false”), it can be useful to convert the user’s input to a boolean to ensure that it is valid.

to post a answer
JavaScript

5 Replies

Copy linkTweet thisAlerts:
@JaySODec 06.2022 — You can use the == and != operators to compare the string to the boolean values true and false. If the string is equal to the string "true" (using the == operator), then it will be considered a truthy value and will be converted to true. If the string is equal to the string "false" (using the == operator), then it will be considered a falsy value and will be converted to false.

Here is an example of how you can use the == and != operators to convert a string to a boolean:


const str = "true";
const bool = str == "true"; // bool will be true



In this example, the str variable is compared to the string "true" using the == operator. Since the str variable has the same value as the string "true", the expression will evaluate to true, and the bool variable will be assigned the value true.

You can also use the === and !== operators, which are strict versions of the == and != operators. These operators compare not only the values of the operands, but also their types. If the string is equal to the string "true" (using the === operator), and the type of the string is the same as the type of the boolean true (which is a boolean), then the string will be considered a truthy value and will be converted to true. If the string is equal to the string "false" (using the === operator), and the type of the string is the same as the type of the boolean false (which is a boolean), then the string will be considered a falsy value and will be converted to false.

Here is an example of how you can use the === and !== operators to convert a string to a boolean:


const str = "true";
const bool = str === "true"; // bool will be true


The str variable is compared to the string "true" using the === operator. Since the str variable has the same value and type as the string "true", the expression will evaluate to true, and the bool variable will be assigned the value true.

Note that if the string does not have the value "true" or "false", then it will be considered a falsy value and will be converted to false using either the == or === operators. You can use the || operator to provide a default value if the string is not "true" or "false".

Here is an example using the || operator:


const str = "maybe";
const bool = str == "true" || false; // bool will be false
Copy linkTweet thisAlerts:
@mariojp26Dec 11.2022 — To convert a string to a boolean in JavaScript, you can use the Boolean() function.

var str = "true";
var bool = Boolean(str);

Alternatively, you can use the !! operator to convert a string to a boolean. This operator converts any value to a boolean.

var str = "true";
var bool = !!str;

In this case, bool would be set to true because the string "true" is considered a truthy value in JavaScript.
Copy linkTweet thisAlerts:
Dec 24.2022 — There are a few different ways you can convert a string to a boolean value in JavaScript.

One way is to use the Boolean function, which returns a boolean value based on the truthiness of the value passed to it. For example:

let str = "true";
let bool = Boolean(str); // bool is true

str = "false";
bool = Boolean(str); // bool is true

str = "";
bool = Boolean(str); // bool is false


Another way is to use comparison operators like == or ===. These operators will return a boolean value based on whether the two values being compared are equal:

let str = "true";
let bool = (str == true); // bool is true

str = "false";
bool = (str == false); // bool is false

str = "";
bool = (str == false); // bool is true


It's important to note that using comparison operators like this can sometimes produce unexpected results, because the comparison is being made between a string and a boolean value. In the example above, the string "false" is not equal to the boolean value false, so the comparison returns false.

If you want to explicitly convert a string to a boolean value, a more reliable approach is to use the JSON.parse() function, which parses a string as JSON and returns the resulting value. For example:

let str = "true";
let bool = JSON.parse(str); // bool is true

str = "false";
bool = JSON.parse(str); // bool is false

str = "";
bool = JSON.parse(str); // bool is null


This method is more reliable because it correctly handles strings that contain the string values "true" and "false", as well as other JSON data types like null, undefined, and 0.
Copy linkTweet thisAlerts:
@codiJan 04.2023 — There are several ways to convert a string to a boolean value in JavaScript. Here are a few options:

Using the Boolean() function:

let str = "true";
let bool = Boolean(str);
console.log(bool); // Output: true

Using the !! operator:

let str = "true";
let bool = !!str;
console.log(bool); // Output: true

Using an if statement:

let str = "true";
let bool;
if (str === "true") {
bool = true;
} else {
bool = false;
}
console.log(bool); // Output: true

Note that the Boolean() function and the !! operator will treat any non-empty string as true, while the if statement will only consider the string "true" to be true.

It's also worth noting that JavaScript is case-sensitive, so the string "TRUE" or "True" will not be considered equal to "true" in an if statement.
Copy linkTweet thisAlerts:
@mjenkinsJan 27.2023 — Assuming that you want the boolean to be false any time the string is not true/TRUE/True/etc., it is a best practice to use the strict equality operator.

var value = 'True';
var myBool = (value.toLowerCase() === 'true'); // myBool is true


You could also use the == operator. But it is a better habit to use === wherever appropriate.

Do not use Boolean() because this will return true unless the argument is: undefined, null, 0, -0, or NaN

var value = 'false';
var myBool = Boolean(value); // myBool is true!
×

Success!

Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...