How To Convert String To Boolean In Javascript

How to convert string to boolean in javascript
Table of Contents

In this tutorial guide I willl show you How To Convert String To Boolean In Javascript. If you are looking for a way to convert a string to boolean in Javascript then you are at the right place.

I am going to show you 4 methods of converting a string to boolean in Javascript. All these methods which I am going to tell you are simple and easy so read this guide till the end.

4 Ways to Convert A String To Boolean In Javascript

As I have said I will show you 4 methods to convert a string to boolean in javascript you can use any method which you find simple and easy to do.

Here are the 4 ways to convert a string to boolean in Javascript:

  • Using the test() function in Javascript
  • Using the comparison operator in Javascript
  • Using ternary operator in Javascript
  • Using JSON.parse() method in Javascript

Now let’s see how to use this methods. I will provide you with code and show you step by step tutorial of each method so read thoroughly.

1. Using the test() function in Javascript

				
					let stringVar = 'true'; 
let boolVar = (/true/i).test(stringValue)
console.log(boolVar) // returns true

let stringVar = 'false'; 
let boolVar = (/true/i).test(stringValue)
console.log(boolVar) // returns false
				
			

The first way to convert a string to boolean is using the test() function. This function checks if the string value is there in the regular experession and if it is there it will return true and if it is not there it will return false.

/i at the end of regular expression is for case insensitive match. Want to know more about the test() function check this guide Javascript test() function.

2. Using the comparison operator in Javascript

				
					let stringVar = 'true'; 
let boolVar = (stringVar == 'true');
console.log(boolVar) //returns true

let stringVar = 'false'; 
let boolVar = (stringVar == 'true');
console.log(boolVar) //returns false
				
			

The second method is using the comparison operators so we have a stringVar which has the string value ‘true’ then we have a boolVar in that we check if the stringVar is equal to the value ‘true’

If the value matches it will set the value of boolVar to true and if does not matches it will set it to false. As you can see we have used the javascript comparison operator ”==” to compare the values. 

3. Using the ternary operator in Javascript

				
					let stringVar = 'true'; 
let boolVar = stringVar.toLowerCase() == 'true' ? true : false; 

console.log(boolVar)//returns true

let stringVar = 'false'; 
let boolVar = stringVar.toLowerCase() == 'true' ? true : false; 

console.log(boolVar)//returns false
				
			

Another way of converting a string to boolean by using ternary operators in Javascript. This is also similar to the second method in this we just use ternary operators if the values are matche then it will return true or else false.

4. Using the JSON.parse() method in Javascript

				
					let stringVar = 'true'; 
let boolVar = JSON.parse(stringVar); 

console.log(boolVar)//returns true

let stringVar = 'false'; 
let boolVar = JSON.parse(stringVar); 

console.log(boolVar)//returns false
				
			

This is one of the easiest method to convert a string to boolean in javascript we will use JSON to convert string to boolean. The JSON.parse() method parses a string as JSON and converts it into an object corresponding to the given text.

So if the value of the string is true then the boolVar will be converted to boolean true or if value is false it will converted to false.

Video tutorial on converting a string to boolean in Javascript

I hope you found this guide helpful and you found the solution to your problem which was converting a string to bool in Javascript. If you want more guides like this join our Telegram channel for future updates – JOIN TELEGRAM CHANNEL.

Here are some more guides on Javascript:

Thank you for reading, Have a nice day 🙂

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles