• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / Using JavaScript to Convert String to Boolean

Using JavaScript to Convert String to Boolean

September 23, 2022 Leave a Comment

We can use JavaScript to convert a string variable to a boolean value by using the JavaScript Boolean() function. Non-empty strings are converted to true and empty strings are converted to false.

var some_string = "This is a string";
var empty_string = "";

console.log(Boolean(some_string));
console.log(Boolean(empty_string));

#Output:
true
false

If you just want to check if a string is equal to “true”, then you can perform a check with the equality operator ===.

var some_string = "true";

console.log(some_string === "true");

#Output:
true

When working with different variable types in JavaScript, the ability to convert variables to other variable types easily is valuable.

One such case is if you want to convert a string to a boolean value.

To convert a string variable to a boolean value in JavaScript, you can use the JavaScript Boolean() function.

Non-empty strings are converted to true and empty strings are converted to false.

Below shows you a simple example of how you can convert strings to boolean values with Boolean() in JavaScript.

var string1 = "true";
var string2 = "false";
var string3 = "   hello";
var string4 = "";

console.log(Boolean(string1));
console.log(Boolean(string2));
console.log(Boolean(string3));
console.log(Boolean(string4));

#Output:
true
true
true
false

Checking if a String is Equal to true in JavaScript

If you just want to check if a string is equal to “true”, then you can perform a check with the equality operator ===.

This can be the case if you have user input and want to see if the user input “true” or “false”.

As we know from above, a string with value “false” will resolve to true when converted to a boolean value.

Therefore, we just need to do a simple check to verify the value of the string variable.

Below is a simple example showing you how to check if a string is equal to “true” in JavaScript.

var some_string = "true";

console.log(some_string === "true");

#Output:
true

Hopefully this article has been useful for you to learn how to use JavaScript to convert string to bool.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Run a Function Every 5 seconds
  • 2.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 3.  setInterval JavaScript – How to Repeat Code in Set Time Intervals
  • 4.  Using JavaScript to Square a Number
  • 5.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 6.  Check That String Does Not Contain Character in JavaScript
  • 7.  Using JavaScript to Scroll to Bottom of Div
  • 8.  Using JavaScript to Get the Decimal Part of a Number
  • 9.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 10.  Using JavaScript to Remove Forward Slash From String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy