It seems that:
if (typeof a == 'undefined') {
a = 0;
}
and
(typeof a != 'undefined') || (a = 0)
has the same effect in Javascript.
I really like the second one because it is short, one line code, but is this legal, and cross browser valid?
I mean, jslint says it has errors. Should I use it without concerns?
IMHO
|| (a = 0)is way too similar to|| (a == 0)and thus confusing. One day overzealous developer will just “fix it“, changing the meaning of your code. And every other developer will have to sit for a while to figure out whether this was your intent or just a simple bug.And this is in fact what JSLint is trying to say:
I avoid using confusing constructs as they hurt readability.
a = a || 0;is way more recognizable and similar in meaning.