

If the string contains the value ‘true’, then it gives us boolean value true else it gives false. How to Convert a String to Boolean in JavaScript Topic: JavaScript / jQuery Prev Next Answer: Use the Operator You can simply use the strict equality operator () if you wants to convert a string representing a boolean value, such as, 'true' or 'false' into an intrinsic Boolean type in JavaScript. In this case, we simply check whether the string contains true or not. The toString () method requires its this value to be a Boolean primitive or wrapper object. For Boolean values, the toString method returns a string representation of the boolean value, which is either 'true' or 'false'. The easiest way to convert string to boolean is to compare the string with 'true': let myBool (myString 'true') For a more case insensitive approach, try: let myBool (myString.toLowerCase() 'true') However, toLowerCase () will throw an error if myString is null or undefined. Testing: var array_1 = new Array(true, 1, "1",-1, "-1", " - 1", "true", "TrUe", " true ", " TrUe", 1/0, "1.5", "1,5", 1.5, 5, -3, -0.1, 0.1, " - 0.1", Infinity, "Infinity", -Infinity, "-Infinity"," - Infinity", " yEs") Use test () Method to Convert String to Boolean Javascript The test () method of Javascript will match a regular expression against a given string. Description The Boolean object overrides the toString method of Object it does not inherit (). var isNum = string.match(/^+$/) != null If equal (type and value both) then the condition is true otherwise false.

If(str.toLowerCase() = 'true' || str.toLowerCase() = 'yes') Javascript let str1 'true' console.log (str1 'true') let str2 'True' console.log (str2.toLowerCase () 'true') Output true true Using JavaScript Operator: This operator compares the equality of two operands with type. The Boolean () function: Boolean (x) uses the same algorithm as above to convert x. version added: 1.4.1 jQuery. Double NOT: x negates x twice, which converts x to a boolean using the same algorithm as above. Blog Plugins Browser Support jQuery.parseJSON () Categories: Deprecated > Deprecated 3.0 Utilities jQuery.parseJSON ( json ) Returns: String or Number or Object or Array or Boolean version deprecated: 3.0 Description: Takes a well-formed JSON string and returns the resulting JavaScript value. Let's start with the shortest, but very strict way: var str = "true" Īnd end with a proper, more tolerant way: var parseBool = function(str) There are two ways to achieve the same effect in JavaScript. After looking at all this code, I feel obligated to post:
