Javascript string to boolean
We may have a boolean value in string format (‘true’ or ‘false’) and we want its boolean equivalent (such as true or false).
Practical application is when we have a check condition value which is stored as a string in database and based on this value we want to perform some operation on our html page (or jsp).
This article will explain 5 different ways to convert string to boolean in javascript with example code.

Method 1 : Using test()

let stringValue = "true"; 
let boolValue = (/true/i).test(stringValue) //returns true

test() method of javascript matches a regular expression against a string.
In this case we check whether the string contains true or not. Now, if the string value is ‘true’, then boolValue will be true, else a false.
/i at the end of regular expression is for case insensitive match.

Method 2 : Using comparison operator
Comparison operator in javascript is a double equal to sign which compares the values of the operands on its right and left sides.
It returns true if both the operands have the same value and false otherwise.

let stringValue = "true"; 
let boolValue = (stringValue =="true"); //returns true

If we use == operator to check the string value against ‘true'(in string format).
If the string value is ‘true’, then the boolean value will also have a true, else a false.
Javascript also has another equality operator, ===. Check this out, if you want to know the difference between == and ===.

Method 3 : Using JSON.parse()

Here we use the parse() method of JSON object.
JSON is a built-in  javascript object and its parse() method parses a string as JSON and converts it into an object corresponding to the given text.
So, if the string value is ‘true’, it is converted to a boolean object with value as true and false otherwise.

let stringValue = "true"; 
let boolValue = JSON.parse(stringValue); //returns true

Tired of reading… Watch video!!!

Method 4 : Using ternary operator

Here, we simply check the string for its equality with ‘true’ and return a boolean true if there is a match and false otherwise using a ternary operator.

let stringValue = "true"; 
let boolValue = stringValue.toLowerCase() == 'true' ? true : false; //returns true

Method 5 : Using switch-case
A switch-case construct is used to cover all possible string value combinations which should lead to a true boolean value.
These string values may be ‘true’, ‘1’, ‘on’, ‘yes’. It also covers boolean true or numeric 1.
If the given string (‘true’) or any other values match, then the boolean true is returned while boolean false is returned in every other case.

let stringValue = "true"; 
let boolValue = getBoolean(stringValue); //returns true 

function getBoolean(value){ 
  switch(value) { 
    case true: 
    case "true": 
    case 1: 
    case "1": 
    case "on": 
    case "yes": 
      return true; 
    default: 
      return false; 
  } 
}

This article explained different methods to parse a string to a boolean in javascript.
Liked it !!! Hit the clap button below.

1
Liked the article ? Spread the word...