469,643 Members | 2,061 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Where are these odd comparison usages useful?

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 */ }

It just depends on how you evaluate this code as to what the outcome is
-- 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?

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Dec 9 '07 #1
3 1032
On Dec 8, 10:00 pm, "-Lost" <maventheextrawo...@techie.comwrote:
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
}

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.

-- 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

Dec 9 '07 #2
AKS
On 9 ÄĹË, 11:00, "-Lost" <maventheextrawo...@techie.comwrote:
>But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior >to me.
I was writing in the same way until I'd been explained (and I got it
right) that it 's too bad.
So the reason is an absence of experience
Dec 9 '07 #3
Response to Peter Michaux <pe**********@gmail.com>:
>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.
That's my inexperience showing in the way in which I word things.

I meant the outcome was dependent on the values of "a" and "b."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Dec 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by ian justice | last post: by
47 posts views Thread by Andrey Tatarinov | last post: by
17 posts views Thread by Jonas Rundberg | last post: by
29 posts views Thread by Steven D'Aprano | last post: by
37 posts views Thread by spam.noam | last post: by
3 posts views Thread by geskerrett | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.