• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  How to Count Vowels in a String Using JavaScript
  • 2.  Loop Through an Array of Objects in JavaScript
  • 3.  Check if a String Contains Uppercase Letters in JavaScript
  • 4.  How to Split a Number into Digits in JavaScript
  • 5.  Remove Commas From a String in JavaScript
  • 6.  Add Years to a Date Using JavaScript
  • 7.  Remove Empty Strings from an Array in JavaScript
  • 8.  Add Minutes to a Date Using JavaScript
  • 9.  How to Swap Values in an Array in JavaScript
  • 10.  JavaScript isInteger – How to Check if a Number is an Integer

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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

x