
This is fine (in fact, lots of JS gurus say you should never use the loose checking operator), but you need to be explicit about it. You are not casting to bool, you are trying to see if a given variable is exactly the string “true”. You are overriding the language’s behavior with your own string-based logic. What you are actually looking for here is strict equality checking. stupid corner case, but for demonstration javascript is a loosely typed language, so this This method disobeys the language’s standard rules: Just to illustrate what is wrong here, javascript has a very simple set of rules to define how variables are cast when doing loose equality checking (=).

#CONVERT STRING TO BOOLEAN TYPESCRIPT CODE#
This makes absolutely no sense and your code will confuse the hell out of anybody who needs to maintain it (including probably yourself). Normally I would just ignore this post but it is well ranked on Google for me so I will post a correction for the sake of others who come here. Just the way I like it! (note: the parentheses are just there for clarity–if you don’t like them or you’re extra extra concerned about line length, removing them won’t cause any errors). Let’s see if we can’t clean it up a bit: myString = (myString = "true") Īhh, nice and clean. Wonderful, it looks like our problem is solved! But, you know something? The above code is kind of messy and a bit long just to check if our string is “true” or not. this evaluates to false, also correct, since myString doesn't equal false. this evaluates to true correctly, myString is true We can do it this way: var myString = "true" Really, the correct way we should be going about this is to check if our string equals “true” - if so, then our string is obviously “true”. 3) Right, let’s try comparing our string against the string “true” Because myString is not an empty string, the Boolean evaluates to true–even if myString equals false. a value that evalutes to false–0, undefined, an empty string, null, etc). Instead, rather misleadingly, it checks whether the variable is a non-falsy value (e.g. or are they? This evaluates to true, although we clearly set it to "false"!Īs you can see, if(Boolean(“false”)) evaluates to true–why? When you create a new Boolean object from a string, it doesn’t try to check whether the string equals “true” or “false”. and this one evaluates to false! Our problems are solved!

very good, this if statement evaluates to true correctly! Let’s let some example code do the talking, though, have a look: var myString = "true" Why not try to create a Boolean object from the string? Well, you’d run into an issue similar to the previous problem. 2) What about creating a boolean object from the string? if it’s not undefined or null), not if it evaluates to false. 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. is checking to see if myString *exists*, not if it's *true*.Īs I mentioned, if(!myString) evaluates to true because that statement only checks if myString exists (e.g. You use this method to convert any string value to a boolean. uh oh! This evaluates to true as well. this should evaluate to true because myString = "true", and it does. However, it’s really the wrong way to go about this. …and this would appear to work, for a while.
#CONVERT STRING TO BOOLEAN TYPESCRIPT UPDATE#
Can't perform a React state update on an unmounted component.Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more.Typescript: Type 'string | undefined' is not assignable to type 'string'.


I know am not the first to ask this and as I mentioned in my title ,I am trying to convert string value boolean.
