&& and ||

F

find clausen

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

-Lost

find clausen said:
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
 
V

VK

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

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

-Lost

VK said:
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
 
V

VK

In JavaScript involved operators precedence is:
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.
 
V

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/library/manuals/2000/javascript/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.aspx> or <http://
ns7.webmasters.com/caspdoc/html/jscript_operator_precedence.htm>

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

Dr J R Stockton

In comp.lang.javascript message <[email protected]
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).
 
J

John G Harris

VK said:
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
 
V

VK

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,362
Latest member
OrderTrimKetoBoost

Latest Threads

Top