Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 21st, 2013, 07:11 PM
 epanagio
Contributing User

Join Date: Nov 2003
Posts: 677
Time spent in forums: 1 Week 3 Days 15 h 34 m 48 sec
Reputation Power: 95
Conditional operator help

I am having a tough time understanding this "simple" conditional:
Quote:
 if ((row != 3) && (i != 9)) { alert('row is not 3 and i is not 9 '); } else alert('row: ' + row + ', i: ' + i);

When variable row equals to 3 and no matter what value variable i is then the if statement is not true.
The way I understand this I should get alert('row is not 3 and i is not 9 ') ONLY id row is 3 and variable i is nine.

HELP!
__________________
Evan

#2
January 21st, 2013, 07:32 PM
 requinix
Forgetful

Join Date: Mar 2007
Location: Washington, USA
Posts: 13,469
Time spent in forums: 5 Months 2 Weeks 2 Days 3 h 58 m 3 sec
Reputation Power: 9259
No*: for the first branch to execute both "row is not 3" and "i is not 9" have to be true. The other branch will execute in the opposite case, namely if "row is not 3" is false or if "i is not 9" is false.
Once you've gotten a handle on that you can start simplifying the logic. Like getting rid of all those "is not"s in favor of just "is"s.

If "row is 3" then "row is not 3" is false and the second branch will execute.

* Everything else you said was right so I think that you typoed the "the way I understand this" statement.

Last edited by requinix : January 21st, 2013 at 07:35 PM.

#3
January 22nd, 2013, 10:49 AM
 ZWEI01
Registered User

Join Date: Jan 2013
Posts: 10
Time spent in forums: 5 h 54 m 13 sec
Reputation Power: 0
'!=' means NOT equal to.

Code:
```x = 10;
x != 10; //returns false             x == 10; //returns true

x = 9;
x != 10; //returns true```

MDN has really great JS resources.
https://developer.mozilla.org/en-US...gical_Operators

 Viewing: Dev Shed Forums > Web Design > JavaScript Development > Conditional operator help