Posted By: Anonymous
Can I write the
if else shorthand without the
var x=1; x==2 ? dosomething() : doNothingButContinueCode();
I’ve noticed putting
null for the else works (but I have no idea why or if that’s a good idea).
This is also an option:
x==2 && dosomething();
dosomething() will only be called if
x==2 is evaluated to true. This is called Short-circuiting.
It is not commonly used in cases like this and you really shouldn’t write code like this. I encourage this simpler approach:
You should write readable code at all times; if you are worried about file size, just create a minified version of it with help of one of the many JS compressors. (e.g Google’s Closure Compiler)