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;
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:
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;
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;
That’s all, folks!
This is coming from my head. Might be wrong. Try it anyways
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');
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 );
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.
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