Sunday, May 31, 2015

Java if vs. Javascript if.

Not all if's are created equal. In Java, you really have to detail exactly what you want to check for. If you put anything inside of an if that's not a boolean comparison, you get an error. Since everything starts out as null, you spend most of your time in Java checking for null (myVar != null).

However, in Javascript, you can do all kinds of fun things with an if!

Clearly the JavaScript if is doing more than simply evaluating boolean expressions. It inherently checks for undefined, null, "", 0, NaN, or false, as well as the classic uses of an if.
This begs the question... Why can't the Java if be more like the JavaScript if? Thousands of checks for null could be removed if the Java if inherently checked for null when presented with a Object.