469,926 Members | 1,692 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

&& and ||

How can I use this:

if (!zxmes && self.name != "menu")

and add if (zmes == 1)

if (!zxmes && self.name != "menu" || zmes == 1)

and make it work.

Jan 28 '07 #1
9 1567
"find clausen" <no@no987.nowrote in message
news:ce********************************@4ax.com...
How can I use this:

if (!zxmes && self.name != "menu")

and add if (zmes == 1)

if (!zxmes && self.name != "menu" || zmes == 1)

and make it work.
There is nothing wrong with that as is. JavaScript is smart enough to know that the first
two terms are tied together via the && so that it becomes:

if ((!zxmes && self.name) || zmes == 1)

If you think this is somehow the culprit you could always just write it (as I have done)
out with additional parenthesis.

To show that your example works though:

var zxmes = null; // satisfies !zxmes
self.name = 'not menu'; // satisfies self.menu != 'menu'
var zmes = 1; // satisifes zmes == 1

With those variables your if statement is always true.

Change zmes to something other than 1, your if is still true.

Change either zxmes to a valid value or self.name to 'menu' and zmes to something other
than 1 and it fails.

-Lost
Jan 28 '07 #2
VK
On Jan 28, 8:58 pm, find clausen <n...@no987.nowrote:
How can I use this:

if (!zxmes && self.name != "menu")

and add if (zmes == 1)

if (!zxmes && self.name != "menu" || zmes == 1)

and make it work.
How do you do 5+3*2 so to add first 5 and 3 and then multiply 2? ;-)
Right, by using parenthesis: (5+3)*2

In JavaScript involved operators precedence is:

....
2) !
....
7) ==
....
11) &&
12) ||
....

That makes pretty clear I believe where are parenthesis going.

Jan 28 '07 #3
VK
JavaScript is smart enough to know that the first
two terms are tied together via the &&
Expression evaluation in programming languages is not an AI or
guessing process ;-) Everything is strictly defined in what is called
"Operator Precedence Table". The only way to break this precedence is
by using parenthesis. && stays by precedence way below ! and != so
grouping parenthesis are needed.

JavaScript operator precedence table consists of 15 positions; I'm
jumping on a chance to post it in full - because however important
this table is many people are not aware of it.

15 positions, from the highest priority to the lowest one:

1) . [] ()
Property accessor, array indexing, function calls, and expression
grouping

2) ++ -- - ~ ! delete new typeof void
Unary operators, return data type, object creation, undefined values

3) * / %
Multiplication, division, modulo division

4) + - +
Addition, subtraction, string concatenation

5) << >>>
Bit shifting

6) < <= = instanceof
Less than, less than or equal, greater than, greater than or equal,
instanceof

7) == != === !==
Equality, inequality, strict equality, and strict inequality

8) &
Bitwise AND

9) ^
Bitwise XOR

10) |
Bitwise OR

11) &&
Logical AND

12) ||
Logical OR

13) ?:
Ternary conditional

14) =
Assignment

15) ,
Comma (multiple evaluation)

Jan 28 '07 #4
"VK" <sc**********@yahoo.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
On Jan 28, 8:58 pm, find clausen <n...@no987.nowrote:
>How can I use this:

if (!zxmes && self.name != "menu")

and add if (zmes == 1)

if (!zxmes && self.name != "menu" || zmes == 1)

and make it work.

How do you do 5+3*2 so to add first 5 and 3 and then multiply 2? ;-)
Right, by using parenthesis: (5+3)*2

In JavaScript involved operators precedence is:

...
2) !
...
7) ==
...
11) &&
12) ||
...

That makes pretty clear I believe where are parenthesis going.
Where did you get your numbers from? From:

http://www.codehouse.com/javascript/precedence/

I get:

....
4. !
....
9. ==
....
13. &&
14. ||
....

Not like it really matters I guess... I am just curious.

-Lost
Jan 28 '07 #5
VK
In JavaScript involved operators precedence is:
>
...
2) !
...
7) ==
...
11) &&
12) ||
...
>Where did you get your numbers from?
Originally (several years ago) from Netscape JavaScript Reference,
then checked against JScript Operator Precedence from MSDN
From: http://www.codehouse.com/javascript/precedence/

I get:

...
4. !
...
9. ==
...
13. &&
14. ||
...
I have no idea where their numbering came from. There are a lot of
most strange resources about JavaScript floating in the Web. :-\

On the first look (did not spend time for the second one :-) they
mixed hell together operator precedence (priority) and operator
associativity - the last one comes into play with equal priority, say
in 2+2-3
This is why they've got such strange order and extra precedence
positions.
>
-Lost
Jan 28 '07 #6
VK
Where did you get your numbers from?
Originally (several years ago) from Netscape JavaScript Reference,
then checked against JScript Operator Precedence from MSDN
Netscape DevEdge is gone long ago, but I believe this was the table I
started with in 1998:
<http://devedge-temp.mozilla.org/libr...avascript/1.3/
guide/expr.html#1008750>

It contains pretty much the same defaults as <http://www.codehouse.com/
javascript/precedence/so I guess it is based on JavaScript 1.3 table

The correct 15 position table I'm using (with associativity taken out
and only priority lleft) is pretty much the same as <http://
msdn2.microsoft.com/en-US/library/z3ks45k7.aspxor <http://
ns7.webmasters.com/caspdoc/html/jscript_operator_precedence.htm>

- with some terms correction (like ?: ternary operator called some
fantastic term "Condition" on MSDN)

Jan 28 '07 #7
In comp.lang.javascript message <11**********************@m58g2000cwm.go
oglegroups.com>, Sun, 28 Jan 2007 10:36:30, VK <sc**********@yahoo.com>
posted:
>How do you do 5+3*2 so to add first 5 and 3 and then multiply 2? ;-)
Right, by using parenthesis: (5+3)*2
Commonly but not necessarily. In RPN, 5 3 + 2 * is 16 and
parentheses (note spelling of plural form) are not needed to control
expression evaluation. With longcalc.exe, via sig line 3,

longcalc 5 3 add 2 mul wrt
- +16

longcalc 987654321 5 bas #ge wrt wrt
- +4 +32

Gregorian Easter Sunday of Gregorian AD 987654321 will be on Month 4 day
32 (to base 5).

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Jan 29 '07 #8
In article <11**********************@p10g2000cwp.googlegroups .com>, VK
<sc**********@yahoo.comwrites
>JavaScript is smart enough to know that the first
two terms are tied together via the &&

Expression evaluation in programming languages is not an AI or
guessing process ;-) Everything is strictly defined in what is called
"Operator Precedence Table".
<snip>

It's the syntax specification that 'strictly defines' the meaning of
expressions. That's things like

ConditionalExpression :
LogicalORExpression
LogicalORExpression ? AssignmentExpression : AssignmentExpression

A precedence table is a useful memory aid, but it doesn't always give
the right answer.
>13) ?:
Ternary conditional

14) =
Assignment

15) ,
Comma (multiple evaluation)
The conditional operator causes problems for precedence tables (also in
other languages). For instance, how about

a = b ? c = d : e = f;

The precedence numbers don't work here. You have to go back to the
syntax specification.

John
--
John Harris
Jan 29 '07 #9
VK


On Jan 29, 11:32 pm, John G Harris <j...@nospam.demon.co.ukwrote:
Comma (multiple evaluation)The conditional operator causes problems for precedence tables (also in
other languages). For instance, how about

a = b ? c = d : e = f;
Easy to check:

<script>
var a = 0;
var b = 1;
var c = 2;
var d = 3;
var e = 4;
var f = 5;

var g = a = b ? c = d : e = f;

alert(d); // 3
</script>

Ternary conditional has higher priority than assignment (13th position
against 14th), so it is evaluated first and only then assigned to g.

Actually ternary conditional implementation in javascript is the most
robust - because it was made with all oops in C/C++/Java in mind.

Jan 29 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Collin VanDyck | last post: by
reply views Thread by Thomas Scheffler | last post: by
11 posts views Thread by Jeremy | last post: by
14 posts views Thread by Arne | last post: by
7 posts views Thread by John Nagle | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.