#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    698
    Rep Power
    95

    Conditional operator help


    I am having a tough time understanding this "simple" conditional:
    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. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,066
    Rep Power
    9398
    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 06:35 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep 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/...ical_Operators

IMN logo majestic logo threadwatch logo seochat tools logo