Users Online

· Guests Online: 40

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

JavaScript String to Boolean

JavaScript String to Boolean

javascript string to boolean

Introduction to JavaScript String to Boolean

We may come across a situation while coding in javascript when we want to retrieve the Boolean value from the string which stores the values like “true”, “false”, “yes”, “no”, “1”, “0”, “on” or “off”. We can use many methods to achieve our goal to obtain a Boolean from the string. However, we should be careful while implementing our logic as some of the thoughtful ways such as using the Boolean() function call and passing our string parameter to it may give unexpected results. Another trick of double negating the string value like !! “false” will give the result true which is not the expected result. In this article, we will learn different methods using which we can efficiently and accurately retrieve the Booleans from the strings.

In real-time situations, this occurs usually when we want to get the values stored in our databases in the string format and after getting its Boolean value deciding what steps should be taken further for the next task to be occurred depending on a retrieved Boolean value.

Here’s the list of possible ways that we can use to get the Boolean value stored in string format.

  • Using comparison operator
  • Using the test method and regular expressions
  • Using the JSON.parse method
  • Using ternary operator
  • Using switch statement

Methods of JavaScript String to Boolean with Examples

We will discuss all the methods one by one and understand their implementation with the help of examples in the further section.

1. Using Comparison Operator

We can use “==” or “===” to compare the string variable with string literals “true” and “false” to confirm the Boolean value of the string variable and then store the resultant obtained from the comparison in a Boolean variable. It may be possible that the string variable stored may have uppercase letters or space in between. In this case, we need to be extra careful while comparing. We should use in-built JavaScript functions like to lowercase() to convert the string to its lower case equivalent and trim() to remove the spaces present in between the string. Let us see with the help of an example, how we can achieve this.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<title>Demonstration of converting a string to its equivalent boolean</title>
</head>
<body>
<h2>Demonstration of converting a string to its equivalent boolean using comparison operator</h2>
</body>
<script>
var sampleBooleanString1 = "true";
var booleanValue1 = (sampleBooleanString1 =="true");
document.write("Obtaining boolean value by using comparison operator "+booleanValue1+" .Type of variable is "+typeof(booleanValue1)+"</br></br>");
var sampleBooleanString2 = " True ";
var booleanValue2 = (sampleBooleanString2.toLowerCase().trim() =="true");
document.write("Converting to lowercase and triming and then comparing the value for comparison "+booleanValue2+" .Type of variable is "+typeof(booleanValue2));
</script>
</html>

Output:

Javascript String to Boolean-1.1

2. Using the Test Method and Regular Expressions

Another way of obtaining the Boolean value from the string is by using the test() built-in method of javascript. The test method checks for the specified regular expression in its parameter and parses the string through the regular expression. If the string is parsable then it returns true otherwise false. This returned value from the test function is stored in the Boolean variable which represents the Boolean equivalent of the string. Here is an example demonstrating how we can do so.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<title>Demonstration of converting a string to its equivalent boolean</title>
</head>
<body>
<h2>Demonstration of converting a string to its equivalent boolean using test method</h2>
</body>
<script>
var sampleString1 = "true";
var booleanValue1 = (/true/i).test(sampleString1);
document.write("Value of Converted String to boolean "+booleanValue1+" .Type of variable is "+typeof(booleanValue1)+"</br></br>");
var sampleString2 = "false";
var booleanValue2 = (/true/i).test(sampleString2);
document.write("Value of Converted String to boolean "+booleanValue2+" .Type of variable is "+typeof(booleanValue2));
</script>
</html>

Output:

Javascript String to Boolean-1.2

3. Using the JSON. parse Method

In case, if the string is in the JSON format then the results are not appropriate if we use the above methods to retrieve the Boolean value. For this, we need to use JSON.parse() method which parses the JSON string according to the string specified in its parameter and the result of parsing can be stored in the Boolean variable. Let us see an example of the same.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<title>Demonstration of converting a string to its equivalent boolean</title>
</head>
<body>
<h2>Demonstration of converting a string to its equivalent boolean using JSON.parse method</h2>
</body>
<script>
var sampleString1 = "true";
var booleanValue1 = JSON.parse(sampleString1);
document.write("Value of Converted String to boolean "+booleanValue1+" .Type of variable is "+typeof(booleanValue1)+"</br></br>");
var sampleString2 = "false";
var booleanValue2 = JSON.parse(sampleString2);
document.write("Value of Converted String to boolean "+booleanValue2+" .Type of variable is "+typeof(booleanValue2));
</script>
</html>

Output:

Output-1.3

4. Using Ternary Operator

Another way of retrieving a Boolean value from the string is by using the ternary operator where resultant of the condition mentioned can further decide what we have to do if it results in true or false. We will return true if our condition of comparing our string with string literal “true” evaluates to true else false and store this variable in a Boolean variable according to our necessity. Here is an example where we can use the same.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<title>Demonstration of converting a string to its equivalent boolean</title>
</head>
<body>
<h2>Demonstration of converting a string to its equivalent boolean using ternary operator</h2>
</body>
<script>
var sampleString1 = "true";
var booleanValue1 = sampleString1.toLowerCase() == 'true' ? true : false;
document.write("Value of Converted String to boolean "+booleanValue1+" .Type of variable is "+typeof(booleanValue1)+"</br></br>");
var sampleString2 = "false";
var booleanValue2 = sampleString2.toLowerCase() == 'true' ? true : false;
document.write("Value of Converted String to boolean "+booleanValue2+" .Type of variable is "+typeof(booleanValue2));
</script>
</html>

Output:

Output-1.4

5. Using Switch Statement

If our string variable may have different values like “1”,” true”,” True”,” TRUE”,” on” and so on which needs to be referred as true in the Boolean variable otherwise false then we can use switch statement in javascript to return true for multiple possible cases of the string value and in the default where none of the above cases will match we will return false. Let us see an example of how we can use the switch statement to get the Boolean value from the string.

Example

Code:

<!DOCTYPE html>
<html>
<head>
<title>Demonstration of converting a string to its equivalent boolean</title>
</head>
<body>
<h2>Demonstration of converting a string to its equivalent boolean using switch statement</h2>
</body>
<script>
function convertToBoolean(sampleValue){
switch(sampleValue){
case true:
case "true":
case 1:
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
}
var sampleString1 = "true";
var booleanValue1 = convertToBoolean(sampleString1);
document.write("Value of Converted String to boolean "+booleanValue1+" .Type of variable is "+typeof(booleanValue1)+"</br></br>");
var sampleString2 = "false";
var booleanValue2 = convertToBoolean(sampleString2);
document.write("Value of Converted String to boolean "+booleanValue2+" .Type of variable is "+typeof(booleanValue2));
</script>
</html>

Output:

Output-1.5

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.97 seconds
10,843,156 unique visits