7 Cool JavaScript Expressions You Should Never Use

I think we can all agree that JavaScript is a pretty cool language. And as with many things, some bits of JavaScript are cooler than others. However, the cool-ness of a piece of JavaScript code is by no means synonymous with its practicality. In fact, one might go so far as to say that the coolness of a JavaScript statement is an inverse measure of how often it should be used. That is, the cooler the code is, the less you should use it.

And if that’s the case, then you should never, ever use anything you see in this article, because from here on out you will be exposed to some Seriously Cool Code.

1) Truncating numbers with bitwise operators

You can use bitwise operators to truncate numbers, like so:

num = ~~num;
num = num#
num = num|num;

Each of these is roughly equivalent to using parseInt or floor on a number, and will all yield the same result. They behave like Math.floor(num) would, assuming the number is positive. They always behave like parseInt(num, 10). Of the three, the last appears to be the fastest.

2) While loop with the iterator in the condition

// while loop with iterator as condition
var i = 5;
while(i--) {


This is a reverse for loop with what appears to be no conditional logic! Is it infinite? Nope. Since 0 is a falsy value, when i– results in zero, the loop will execute a final time and quit. It’s quick, easy, and the fastest loop currently known.

3) If statements without the if

someBoolean && (someStatement = 10);

Okay, what’s going on here? We’re taking advantage of the fact that when parsing conditional statements, the return value of the statement is evaluated. We also know that if the left hand statement of an && operator is evaluated as truthy, the right hand will be evaluated as well; otherwise, it won’t be. So this code is essentially equivalent to this:

if(someBoolean) {
someStatement = 10;

4) Comma operator

var a = 1,
b = 2;

b = (a++, 10);

The little known comma operator can be a bit mysterious and tricky to understand, but once you know what it means, it can be rather useful. Note, however, that useful is not equivalent with maintainable or understandable, and both of those take precedence over “useful”. Anyways, the comma operator performs all statements enumerated and returns the last statement’s value.

That means, in the above code, “a” will end up equaling 2, and “b” will end up equaling 10; the two statements in the last line are evaluated, and the return value of the last one is passed on.

5) The double exclamation point!!

value = !!value;

Using a double exclamation point will instantly identify you as an over-excited internet poster under-familiar with the colloquial usage of English. (unless you’re writing in JavaScript, in which case it’s perfectly ok). The double exclamation point, or double bang, converts a truthy or falsy value to “true” or “false”. In other words, it operates exactly like Boolean(value) would.

6) Define value if undefined

value = value || "hello world";

This function is quite simple. It defines a value if it appears to be undefined. That is, if the value already evaluates to a falsy value (which by some strange twist of fate, “undefined” does), it’s replaced by whatever comes after the OR operator. Note: the following is a much safer, but slightly longer, way of ensuring the same thing:

value = (typeof value !== "undefined") ? value : "hello world";

The above code checks to see if “value” is literally exactly equal to undefined, and will not fail if “value” does not exist anywhere in the code and is unrecognized in addition to being undefined. However, it is a mouthful. Or lineful, dare I add.

7) The void operator

undefined = void 0;

There is an operator called void. It evaluates whatever expression is passed to it and then returns a true, unadulterated value of undefined. This is useful in cases where you suspect that undefined may in fact have been defined at some point (and yes, that is legal in JavaScript). You can also compare directly against void 0 if you don’t want to overwrite whatever definition undefined has.

That’s all, folks!

I think that’s a sufficient amount of coolness in JavaScript for now, I don’t want to freeze anybody out. And, having read this article, keep in mind that you should never, ever use any of the code I’ve demonstrated for you above.

But, since I know you’ll ignore me, at least just remember to comment your super-cool JavaScript code. If not for yourself, at least for the sake of the next developer that comes along, who, being naturally less cool than you, will be unable to understand it.

And please, if you have any more cool, comment-worthy JavaScript, be sure to leave it below in the comments or shoot me an email–I’d love to see it.

9 thoughts on “7 Cool JavaScript Expressions You Should Never Use”

  1. yeebus says:

    You write about a lot of this as if it is purely JavaScript when half of these concepts have been around for decades and used heavily and frequently in C-style languages. (i.e. 3, 5, 7 (void operator))

  2. admin says:

    Hi yeebus,

    I haven’t implied anywhere that these are JavaScript-specific expressions; merely that they are expressions in JavaScript which I find to be interesting. I’m aware that many of the same concepts I have enumerated can be found in other languages; however, this is primarily a JavaScript blog, which is why I have chosen not to mention them.

  3. vedran says:

    Var a;


    This is coming from my head. Might be wrong. Try it anyways :)

  4. Chief says:

    I disagree with your #6, I don’t think there is necessarily wrong with using the double logical NOT operator as a means of casting truthy or falsy values to their boolean equivalents. The potential for silly errors is much greater with the Boolean object, consider the following:

    var a = new Boolean(false);
    console.log('This makes me sad');

    1. Oshimin says:

      var a = new Boolean(false); // a is an objet with a primitive value is not a boolean
      // use if(a.valueOf()) instead of if(a)
      console.log(‘This makes me sad’, typeof a /* log ‘object’ not ‘boolean’ */, !!a !== false );

  5. JM says:

    I see #3 in a lot of JavaScript libraries…

    1. elliotbonneville says:

      And more’s the pity. :)

      …to be fair, it is somewhat less evil than the others on the list. I still wouldn’t use it, though.

  6. Oshimin says:

    Sorry i speak french.

    The comma operator is fun for switch variable, like this;
    var a = 1, b=0, tmp;
    // without comma operator
    tmp = a;
    b = a;
    b = tmp;

    // with comma operator
    b = (tmp=a, a=b,tmp) // very tiny