Saturday, October 25, 2025
HomeLanguagesJavascriptJavaScript Convert a string to boolean

JavaScript Convert a string to boolean

Sometimes it is needed to convert a string representing a boolean value “true”, or “false” into the intrinsic type of JavaScript. In this article, we have given a string and the task is to convert the given string to its boolean value.

There are two methods to do so:

Using JavaScript == Operator: This operator compares the equality of two operands. If equal then the condition is true otherwise false. 

Example: This example uses the == operator to convert a string to its boolean value. 

Javascript




let str1 = "true";
console.log(str1 == 'true');
 
let str2 = "True";
console.log(str2.toLowerCase() == 'true');


Output

true
true

Using JavaScript === Operator: This operator compares the equality of two operands with type. If equal(type and value both) then the condition is true otherwise false. 

Example: This example uses the === operator to convert the string to its boolean value. 

Javascript




let str = "true";
console.log(str === 'true');
 
str = "True";
console.log(str.toLowerCase() === 'true');


Output

true
true

Approach 3: Using the Boolean() function

The boolean function returns the boolean value of the variable. It can also be used to find the boolean result of a condition, expression, etc. The Boolean() function evaluates the input and returns a boolean value. In this case, since the input string “true” is non-empty, the Boolean() function returns true.

Example: In this example, we are using the above-explained approach.

Javascript




let str = "true";
let result = Boolean(str);
console.log(result);


Output

true

Approach 4: Using a regular expression

In this approach regular expression to convert a string to a boolean value in JavaScript.

Example: In this example, the regular expression /^true$/i is used with the test() method to check if the string matches the pattern “true” (case-insensitive). The ^ symbol denotes the start of the string, $ denotes the end of the string, and the i flag makes the pattern case-insensitive.

Javascript




let str = "true";
let boolValue = /^true$/i.test(str);
console.log(boolValue);


Output

true

Approach 5: Using the !! (double negation) operator:

The !! (double negation) operator is a commonly used approach to convert a string to a boolean value in JavaScript

Example: In this example, The first negation (!) converts the string into its opposite boolean value, and the second negation (!) reverts it back to the original boolean value.

Javascript




let str = "true";
let boolValue = !!str;
console.log(boolValue);


Output

true

Approach 6: Using JSON.parse():

The JSON.parse() method can be used to convert a string to a boolean value in JavaScript. JSON.parse() method is used to parse the string “true” and convert it into its corresponding boolean value, which is true.

Example: In this example, we are using the above-explained approach.

Javascript




let str = "true";
let boolValue = JSON.parse(str);
console.log(boolValue);


Output

true

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS