LA.NET [EN]

Sep 14

I’m assuming that you’ve already seen code which looks like this:

function doSomething(isTrue) {
    if (!!isTrue ) {
       //some code here
    }
}

And I’m sure you’re wondering what’s going on there. This is sometimes called boolean normalization and its main advantage is that you end up getting a true boolean value instead of relying on the use of truthly and falsy values in JavaScript. Lets walk this step by step, shall we? When you have something like this:

var aux = !isTrue;

you’re using the ! operator to “negate” the current expression. As you know by now, JavaScript has several falsy values (ie, values which represent false –ex.: 0 is always converted into false). When we apply the ! operator for the first time, we end up converting the isTrue variable to a boolean and negating it. The second ! ends up negating that intermediate expression and we end up with the correct boolean value for the initial expression. Many consider this a bad practice and prefer to use other approaches. For instance, you could get the same result by using the Boolean function:

function doSomething(isTrue) {
    if (Boolean(isTrue)) {
        alert("YES!");
    }
}

Btw, don’t fall to the temptation of using a Boolean object, ie, don’t call new Boolean(isTrue) for converting the value to a boolean. If you do that, you’ll end up creating a new *object* which wraps a boolean value. Since we’re talking about a non null value, you’ll always end with a true expression and this is not what we want here!

Even though the code is somewhat cryptic and consider by many as a “poor man’s boolean conversion operator”, it’s code that most savvy JS programmers use because it’s compact and simple. I’ve been using this technique whenever I need to ensure the use of a proper boolean value. And that’s all for today. Stay tuned for more on JavaScript.

4 comments so far

  1. sc
    5:53 am - 9-15-2009
  2. luisabreu
    8:31 am - 9-15-2009

    I agree with the critics on the example (yes, it could be better!). but I don”t have lots of time to write complex examples. The main objectives of my posts are to introduce concepts and I guess that I”ve achieved that with this post.

    btw, I never said that I use the !! in ifs (even though I shouldn”t, the truth is that most of the time I do end up relying in JS” truthy and falsy values). The only thing I said was that I”ll use this technique whenever I need to get a boolean.

    I”m not sure that this is WTF material. In fact, I”d say that using *falsey* is more WTF than this post, but that”s only my opinion.

    thanks for the feedback.

  3. Rob Thijssen
    2:01 pm - 9-15-2009

    For what it”s worth, the post makes perfect sense to me. The example uses an if statement because it”s the most obvious way to convey the message about what you”re evaluating after the type conversion. It”s only a wtf candidate if you don”t undertand type safety.

  4. Polprav
    7:55 pm - 10-15-2009

    Hello from Russia!
    Can I quote a post in your blog with the link to you?

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>