![]() ![]() 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 (=). 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. 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. 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. 1) You might be tempted to say if(myString)… ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |