How to specify (and/or) conditions in Javascript?

Discussion in 'HTML' started by Fernie, Nov 19, 2004.

  1. Fernie

    Fernie Guest

    How do you specify an and/or condition in javascript? I can specify a
    condition as follows:

    if (var = 'text1')
    {
    alert('the variable is equal to text1');
    }


    What I'd like to do is add an 'or' condtion but I get a syntax error:

    if (var = 'text1' or var = 'text2')
    {
    alert('the variable is equal to either text1 or text2');
    }


    Thanks in Advance,

    Fernie



    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Nov 19, 2004
    #1
    1. Advertising

  2. Fernie

    Philip Ronan Guest

    Fernie wrote:

    > How do you specify an and/or condition in javascript? I can specify a
    > condition as follows:
    >
    > if (var = 'text1')
    > {
    > alert('the variable is equal to text1');
    > }


    YOU WRONG!!!

    if (var == 'text1')
    {
    alert('the variable is equal to text1');
    }


    >
    > What I'd like to do is add an 'or' condtion but I get a syntax error:
    >
    > if (var = 'text1' or var = 'text2')
    > {
    > alert('the variable is equal to either text1 or text2');
    > }


    YOU WRONGER!!!

    if (var == 'text1' || var == 'text2')
    {
    alert('the variable is equal to either text1 or text2');
    }

    (When's "Banzai!" coming back to Channel 4, I wonder?)

    --
    Philip Ronan

    (Please remove the "z"s if replying by email)
     
    Philip Ronan, Nov 19, 2004
    #2
    1. Advertising

  3. Fernie

    Fernie Guest

    Philip,

    Thank you for correcting me.

    Best Regards,

    Fernie


    "Philip Ronan" <> wrote in message
    news:BDC3DEC2.25C80%...
    > Fernie wrote:
    >
    >> How do you specify an and/or condition in javascript? I can specify a
    >> condition as follows:
    >>
    >> if (var = 'text1')
    >> {
    >> alert('the variable is equal to text1');
    >> }

    >
    > YOU WRONG!!!
    >
    > if (var == 'text1')
    > {
    > alert('the variable is equal to text1');
    > }
    >
    >
    >>
    >> What I'd like to do is add an 'or' condtion but I get a syntax error:
    >>
    >> if (var = 'text1' or var = 'text2')
    >> {
    >> alert('the variable is equal to either text1 or text2');
    >> }

    >
    > YOU WRONGER!!!
    >
    > if (var == 'text1' || var == 'text2')
    > {
    > alert('the variable is equal to either text1 or text2');
    > }
    >
    > (When's "Banzai!" coming back to Channel 4, I wonder?)
    >
    > --
    > Philip Ronan
    >
    > (Please remove the "z"s if replying by email)
    >
    >




    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Nov 19, 2004
    #3
  4. Fernie

    C A Upsdell Guest

    "Philip Ronan" <> wrote in message
    news:BDC3DEC2.25C80%...
    > Fernie wrote:
    >> What I'd like to do is add an 'or' condtion but I get a syntax error:
    >>
    >> if (var = 'text1' or var = 'text2')
    >> {
    >> alert('the variable is equal to either text1 or text2');
    >> }

    >
    > YOU WRONGER!!!
    >
    > if (var == 'text1' || var == 'text2')
    > {
    > alert('the variable is equal to either text1 or text2');
    > }


    Better would be:

    if ( (var == 'text1') || (var == 'text2') )
    {
    alert('the variable is equal to either text1 or text2');
    }

    because, as the OP is a newbie at this, he/she will likely have problems
    regarding which operators bind more closely, and so should get into the
    habit of using parentheses to make sure the code does what they expect, even
    when, as in the example above, extra parentheses are superfluous.
     
    C A Upsdell, Nov 19, 2004
    #4
  5. Fernie

    Fernie Guest

    Thanks for the suggestion.

    >"C A Upsdell" <cupsdell0311XXX@-@> wrote in message
    >news:...
    > Better would be:
    >
    > if ( (var == 'text1') || (var == 'text2') )
    > {
    > alert('the variable is equal to either text1 or text2');
    > }
    >
    > because, as the OP is a newbie at this, he/she will likely have problems
    > regarding which operators bind more closely, and so should get into the
    > habit of using parentheses to make sure the code does what they expect,
    > even when, as in the example above, extra parentheses are superfluous.




    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Nov 19, 2004
    #5
  6. Philip Ronan <> wrote:

    > Fernie wrote:
    >
    > > How do you specify an and/or condition in javascript? I can specify a
    > > condition as follows:
    > >
    > > if (var = 'text1')

    >
    > YOU WRONG!!!


    Easy, big fella... ;-)

    > if (var == 'text1')


    Right. One way of guarding against the relatively common programmer's
    error of typing "=" when "==" is meant, is to make a habit of putting
    the constant (or, the expression that can't be assigned to) on the
    left-hand side of the operator. E.g.,
    if ('text' == var)

    Accidentally writing that as
    if ('text' = var)

    is bound to set off alarms, though I'm not sure if they'd be at
    compile-time (hopefully) or later in JavaScript.

    --
    Joel.
     
    Joel Shepherd, Nov 20, 2004
    #6
  7. On Sat, 20 Nov 2004 21:50:31 GMT, Joel Shepherd <>
    wrote:

    [snip]

    > Accidentally writing [...]
    > if ('text' = var)
    >
    > is bound to set off alarms, though I'm not sure if they'd be at
    > compile-time (hopefully) or later in JavaScript.


    ECMAScript is an interpreted language, so there is no compilation phase.
    Fatal syntax errors should be signaled when the script is parsed as the
    document loads.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Nov 20, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabriel Lozano-Morán

    How to loop a recordset and display html with conditions

    Gabriel Lozano-Morán, Jul 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    561
    Cactus Corp.
    Jul 6, 2005
  2. Replies:
    2
    Views:
    1,194
  3. mars
    Replies:
    6
    Views:
    370
    Laurent Pointal
    Feb 7, 2007
  4. Shaldaman

    XML parameters and conditions

    Shaldaman, May 9, 2007, in forum: XML
    Replies:
    1
    Views:
    351
    Joseph Kesselman
    May 9, 2007
  5. Larry Bates

    Drop folder and race conditions

    Larry Bates, Oct 9, 2007, in forum: Python
    Replies:
    1
    Views:
    288
    Steven D'Aprano
    Oct 9, 2007
Loading...

Share This Page