stcho
stcho

Reputation: 2119

How to correctly chain OR and AND conditions in Javascript?

I am trying to write

if (x !== "One" || x !== "Two" || x !== "Three") {
   x = undefined;
}

However, this is not working for me. What is the correct way to get the same result?

It works fine when I only have one condition

if (x !== "One") {
   x = undefined;
}

Upvotes: 1

Views: 4706

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201399

You wanted an and, not an or. And your variable name can't be var.

var t = "One";
if (t !== "One" && t !== "Two" && t !== "Three") {
  t = undefined;
}

Because the previous or(s) would always evaluate to true. if the variable was "a" or var was "One" it's not "Two" or "Three".

Upvotes: 6

Related Questions