So I use the Google Closure Compiler to minify my JavaScript before using it in production. Today, I paid attention to how it minifies and optimizes(?) the code and realized this trend:
Compiling the code below :
function b(a) {
if ( a == null ) {
console.log('ohai');
}
}
resulted in an optimization in the comparison expression where the literal is moved to the left and the variable to the right. As in
function b(a){null==a&&console.log("ohai")}
(Btw, it subsitutes single quotes for double quotes.) The expression remains the same if the other operand is an object. Why is moving the literal to the left of the operator an optimization of the expression?