Click to See Complete Forum and Search --> : is there an "or" reserved word in JavaScript?


omelette
11-18-2003, 02:01 AM
Hi everybody,
I've searched quite a few places and I don't think there is an "or" function in JavaScript. Or am I wrong?

eg. like this:

if (car == "Audi TT") or (car == "BMW Z3") {
window.alert("cool car!");
}
else {
window.alert("great car!");
}

Gollum
11-18-2003, 02:18 AM
You will find that in Javascript, "or" is actually spelt "||". So your statement should look like this...

if ( (car == "Audi TT") || (car == "BMW Z3") )
{
window.alert("cool car!");
}
else
{
window.alert("great car!");
}

And for the record - if statments need parentheses "()" around the whole expression.

fredmv
11-18-2003, 04:23 AM
(car == 'Audi TT') ? alert('Cool car!') : alert('Great car!');

Charles
11-18-2003, 04:40 AM
Originally posted by fredmv
(car == 'Audi TT') ? alert('Cool car!') : alert('Great car!'); 1) That doesn't answer the original question.

2) That is a bit of a mis-use of that syntax. That's an operator; it returns something. It is for something more like:

var m = now.getHours() < 10 ? '0' + now.getHours() : now.getHours()

fredmv
11-18-2003, 04:42 AM
While I realize it doesn't answer the original question, I figured since the previous person who posted did answer it, I would provide an alternative way of doing it. I also agree with you; that is an extreme misuse of the condiontal operator. I was tired at the time of writing and didn't even think of it. I can assure you I know how to correctly use it, however.

omelette
11-24-2003, 12:54 AM
Thanks Gollum, Charles & fredmv, the "||" was exactly what I'm after. Really appreciate all of your input. I don't quite know how to apply fredmv's alternative solution but hopefully, it'll help someone else :).

ray326
11-24-2003, 12:03 PM
Originally posted by Charles
2) That is a bit of a mis-use of that syntax. That's an operator; it returns something. It is for something more like:

var m = now.getHours() < 10 ? '0' + now.getHours() : now.getHours()

True enough but here's a "proper" example within the original context:

window.alert(((car == "Audi TT") || (car == "BMW Z3"))?"Cool car":"Great car");