Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

How to Convert a String to Boolean in JavaScript

  • 11 Sep, 2025
  • Com 0

The most reliable and efficient way to convert a String to a Boolean type is to use the Boolean() constructor and pass the string value to it. The empty string (“”) becomes false, and a non-empty string becomes true.

Converting a String to Boolean in JavaScript using boolean constructor

console.log(Boolean("true"));

// Output: true (non-empty)

console.log(Boolean("false"));

// Output: true (non-empty, unexpected!)

console.log(Boolean(""));

// Output: false

console.log(Boolean("0"));

// Output: true

console.log(Boolean("yes"));

// Output: true

console.log(Boolean(" "));

// Output: true

In this code, “true” and “false” refer to a string and not a Boolean value, so Boolean() returns true in both cases. An empty string is considered false because there is no value.

When it comes to 0, it should return false, but “0” here is a string, so it returns true.

If you pass the space to the Boolean() constructor, it returns true because the space is counted as a character. An empty string and a space are two different things.

Using strict equality comparison (===)

String to Boolean using strict comparison operator in JavaScript

 

The strict equality operator (===) compares the string directly to “true”. If it matches, it returns true; otherwise, it returns false. It is a case-sensitive approach, which is why, in the last part of the above figure, ‘True’ and ‘true’ are not the same and return false.

// Creating a custom arrow function

const strToBool = (str) => str === "true";

console.log(strToBool("true"));

// Output: true

console.log(strToBool("false"));

// Output: false

console.log(strToBool("True"));

// Output: false (case-sensitive)

console.log(strToBoolInsensitive("yes")); 

// Output: false

As you can see, it handles only “true” and “false” values correctly, but what about the synonyms like “yes”? Well, it can’t handle those other synonyms. That is the limitation.

For case-insensitiveness, we can create another custom arrow function that converts the input string to lowercase and then compares it with the “true” value.

const strToBoolInsensitive = (str) => (str || "").toLowerCase() === "true"; 

console.log(strToBoolInsensitive("True")); 

// Output: true 

console.log(strToBoolInsensitive("FALSE")); 

// Output: false

Using JSON.parse()

The JSON.parse() method parses the string as JSON, where “true” becomes true and “false” becomes false. For requiring an exact lowercase match, use .toLowerCase() for case-insensitivity. Wrap in try-catch for safety.

// Creating a custom arrow function

const strToBool = (str) => JSON.parse(str);

console.log(strToBool("true"));

// Output: true

console.log(strToBool("false"));

// Output: false

The above code shows that the JSON.parse() method correctly handles the “true” and “false” values.

For potential errors, we can handle different scenarios using a try-catch block.

// Case-insensitive with error handling
const safeStrToBool = (str) => {
    try {
        return JSON.parse((str || "").toLowerCase());
    } catch {
        return false;  // or throw error
    }
};

console.log(safeStrToBool("True"));

// Output: true

console.log(safeStrToBool("FALSE"));

// Output: false

console.log(safeStrToBool("yes"));

// Output: false (error caught)

In the above code, we handle other truthy or falsy values such as “Yes” or “No”. So, this is also a limitation of this approach.

That’s all!

Post Views: 6
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Find the Sum of an Array of Numbers in JavaScript
How to Add Property to an Object in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend