![]() ![]() However, it's a bit harder to read if you're not familiar with the logical NOT ( !) operator. The double NOT ( !!) operator is quite concise but does the same thing as the Boolean wrapper. When we use the double NOT operator, the values are flipped, meaning we are now performing a pure boolean conversion: let myString1 = !! 'test' // returns true let myString2 = !! '' // returns false Using the double NOT operator is equal to using the logical NOT operator ( !) twice, which means that it inverts the result of the single NOT operator: let myString1 = ! 'test' // returns false let myString2 = ! '' // returns true Secondly, converting a string of "false" to a boolean value of false will fail because any non-empty string converts to true.The first is that this will return true for an empty string with at least one blank character (space, tab, etc.), that is why we have to be cautious when using this method:Ĭonst myString5 = Boolean( ' ') //returns true.In general - empty objects are evaluated to false, and non-empty objects are evaluated to true.Īny string which isn't the empty string will evaluate to true by using the Boolean wrapper: let myString1 = Boolean( 'true') //returns true let myString2 = Boolean( '') // //returns false let myString3 = Boolean( 'false') //returns true let myString4 = Boolean( 'True') //returns true This is done by testing the truthy-falsy value of an object. It is actually an object wrapper for boolean values - it wraps around other objects thus making them a valid boolean value. JavaScript has a built-in Boolean object for storing boolean values. Let boolValue = ( /true/i).test(stringValue) //returns true Using the Boolean Wrapper Class? To fix this, we can add /i at the end of the regular expression to ensure for case-insensitive match: let stringValue = "True" Let boolValue = ( /true/).test(stringValue) //returns false You will notice this is actually case-sensitive, as this will return false if it has slight case inconsistency: let stringValue = "True" Regular expressions (RegEx) are patterns for matching and testing string character combinations. Let boolOutput = myString.toLowerCase() = 'true' ? true : false // returns true Using Regular Expressions (RegEx) All we will do is check if our string is equal to "true" and then return either a boolean value of true if there is a match or false if it doesn't: let myString = "true" We can also spice things up a little by introducing the ternary operator alongside the equality operator. Let boolOutput2 = (myString2 = "true") //returns false Let boolOutput1 = (myString1.toLowerCase() = "true") //returns false let myString = "True" ![]() Let boolOutput1 = (myString1 = "true") //returns false let myString1 = "Test" Let boolOutput = (myString.toLowerCase() = "true") // returns trueĪs we've stated before, the previous code will return false if our string value is not equal to "true": let myString1 = "Test" Additionally, we can convert a string to lowercase first, just to make sure the letter case won't cause any faulty outputs: let myString = "True" ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |