Search Unity

Javascript and testing for 3 values?

Discussion in 'Scripting' started by bigkahuna, Dec 18, 2007.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    What's the best way to test for three values in Javascript, in other words, what I want to do is this:

    Code (csharp):
    1. if (variable1==true  variable2==true  variable3==false) {
    2.   then do this;
    3.   }
    But AFAIK you can't do this in Javascript. So what I've been doing is this:

    Code (csharp):
    1. if (variable1==true  variable2==true) {
    2.   if (variable3==false){
    3.     then do this;
    4.   }}
    But that seems clumsy to me. Is there a better way?
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    The former should be fine. Doesn't it work?

    You can always add brackets if you need to group a set of expressions:

    Code (csharp):
    1. if((variable1 == true  variable2 == true)  variable3 == false)
    2. { ... }
     
  3. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    Also note that since the variable is a boolean it already represents a true/false value so theres no need to do a comparison, simply write:

    Code (csharp):
    1. if (variable1  variable2  !variable3) {
    2.   then do this
    3. }
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Well silly me :oops: I searched every reference on Javascript I could find and couldn't find anywhere that said that it was possible so I assumed it wasn't. I'll give it a go, thanks Neil!

    Edit: Thanks Talzor, that's very clean.