On Dec 8, 10:00 pm, "-Lost" <maventheextrawo...@techie.comwrote:
Quote:
I read on a blog where someone was baffled by the outcome of code such
as:
>
if (a == b == 0) { /* if a AND b == 0, execute */ }
if (a && b == 0) { /* if a AND b == 0, execute */ }
Those /**/ comments are not great.
var a = 0;
var b = 0;
if (a == b == 0) {
alert('first'); // doesn't execute
}
if (a && b == 0) {
alert('second'); // doesn't execute
}
var a = 1;
var b = 0;
if (a == b == 0) {
alert('first'); // does execute
}
if (a && b == 0) {
alert('second'); // does execute
}
Quote:
It just depends on how you evaluate this code as to what the outcome is
You don't choose how to evaluate the code. Hopefully, in any
particular host, the ECMAScript standard has specified the outcome.
Quote:
-- for example the author's comments do not reflect an understanding of
this at all. But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior to
me.
>
Can anyone give me an example where code like this is beneficial?
I would say the first code is a bad practice. The second code could be
written perhaps more clearly as the following.
if (a && (b == 0)) {/* ... */}
--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca