

Convert string to boolean how to#
In this article, you have learned how to convert a string value to a boolean. But this will return true because it only returns false when it’s an empty string. Note: When you convert a string of "false", you’d expect it to return a boolean value of false. The only limitation with this method is that if you add space between the quote to represent string, it will return true - meaning it regards it as a string. When you pass a string value into the Boolean object, it will evaluate to true, but when you pass an empty string, it will evaluate to false.

This method works just like the double NOT operator. The JavaScript Boolean object represents a boolean value. How to Parse a String to a Boolean with a Boolean Wrapper It will only return false when it is an empty string. One disadvantage of this method is that you cannot convert a string of "false" to a boolean value of false. You use this method to convert any string value to a boolean. You use it to invert the result of the single NOT operator: let stringValue1 = !!'true' This is when you can use the double NOT logical operator. Rather, you want to convert a string to a boolean, meaning when the string is empty, it should return false, and in every other case, it should return true. If it’s an empty string, it will return true otherwise, false: let stringValue1 = !'true' When you add the single NOT operator in front of a string, it will either return true or false. You should also know how to use the single NOT operator, which you can use to invert a result. How to Parse a String to a Boolean with the Double NOT Operator ( *!!*) Let boolValue = (/true/i).test(boolString) You will have to add the i flag at the end of the regular expression to allow for a case-insensitive match. This also is case sensitive: let boolString = "True" Let boolValue = (/true/).test(boolString) For example, if you want to test for the true string value, you'd do this: let boolString = "true" It is a vast programming topic and you can use regex as a pattern to match and test string character combinations.Ī very simple guide to regex will tell you that the expression is placed between two slashes ( /). How to Parse a String to a Boolean with Regex Let boolValue = (boolString.toLowerCase() = "true") Īnother method very similar to the identity operator is the regex method, where you can test if two values match. You can fix this using the toLowerCase() method, so it first converts the string value to the letter case that fits your comparison and then compares. This is a strict equality operator and will be strict with letter case comparison: let boolString = "True" If both values are the same, it will return the boolean value true, otherwise, it will return the boolean value false. In this case, you want to convert a string to a boolean, which means you'll compare it to the string "true". This implies that their letter case – and everything else – must also be the same. It will only return true if the two values being compared are the same. The strict operator is another name for the identity operator. How to Parse a String to a Boolean with the Identity Operator ( =) If you are not in a rush, let’s understand each of the methods and lots more. In case you are in a rush, here is how you can do it: // Using identity operatorĬonsole.log((boolString = "true")) // true / falseĬonsole.log((/true/).test(boolString)) // true In this article, you will learn how to convert a string to a boolean value using different methods in JavaScript. When you're manipulating data, receiving values from forms, and dealing with data in other ways, these values may take the incorrect datatype.Īssume you want your value to be a boolean with either true or false, but it is stored as a string – "true" or "false." It becomes challenging to use for your intended purpose, so you must first convert these boolean string values to actual boolean values.
