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

F

Fernie

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
 
P

Philip Ronan

Fernie said:
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?)
 
C

C A Upsdell

Philip Ronan said:
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.
 
F

Fernie

Thanks for the suggestion.
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.
 
J

Joel Shepherd

Philip Ronan said:
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.
 
M

Michael Winter

[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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top