How to convert a string to a boolean (the right way)

There are a couple of ways to convert a string variable to a boolean variable in Javascript. However, when doing this, you have to be kind of careful: it’s kind of easy to mess up this sort of logic and doing it wrong can result in some nasty bugs. So, in order to save you a headache or two, I’ve detailed how to do it properly in this article. Read on, if you’re interested.

1) You might be tempted to say if(myString)…

…and this would appear to work, for a while. However, it’s really thewrong way to go about this. Observe:

var myString = true;
if (myString) {
    // this should evaluate to true because myString = "true", and it does. Good!
if (!myString) {
    // uh oh! This evaluates to true as well. Why? Because if(!myString)
    // is checking to see if myString *exists*, not if it's *true*.

As I mentioned, if(!myString) evaluates to true because that statement only checks if myString exists (e.g. if it’s not undefined or null), not if it evaluates to false.

2) What about creating a boolean object from the string?

Why not try to create a Boolean object from the string? Well, you’d run into an issue similar to the previous problem. Let’s let some example code do the talking, though, have a look:

var myString = "true";
if(Boolean(myString)) {
    // very good, this if statement evaluates to true correctly!
if(!Boolean(myString)) {
    // and this one evaluates to false! Our problems are solved!
var myOtherString = "false";
if(Boolean(myOtherString)) {
    // ...or are they? This evaluates to true, although we clearly set it to "false"!

As 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”. Instead, rather misleadingly, it checks whether the variable is a non-falsy value (e.g. a value that evalutes to false–0, undefined, an empty string, null, etc). Because myString is not an empty string, the Boolean evaluates to true–even if myString equals false.

3) Right, let’s try comparing our string against the string “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”. Otherwise, it must be false. We can do it this way:

var myString = "true";
if(myString == "true") {
    // this evaluates to true correctly, myString is true
if(myString == "false") {
    // this evaluates to false, also correct, since myString doesn't equal false.

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. Let’s see if we can’t clean it up a bit:

myString = (myString == "true");

Ahh, nice and clean. 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).

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *