wielorybgrotolaz
wielorybgrotolaz

Reputation: 25

Add more variables to if statement

I am not a programmer and I know nothing about java script. I am sure that you are already hate me, but I've honestly searched for the answer whole day and I am getting crazy.

I am trying to edit, a part of code on my wordpress website. Here's the code:

if(name != 'name1') return true;

How can I add more variables? I've tried adding || and &&:

if(name != 'name1' || 'name2') return true;

else if:

if(name != 'name1') {
return true;
}
else if(name != 'name2') {
return true;
}

and many more combinations that I was able to find online.

I am sure that it's a simple fix, but I can't spend months of learning javascript to edit a straightforward function on my website.

I would very much appreciate your help.

Upvotes: 1

Views: 1195

Answers (4)

Alberti Buonarroti
Alberti Buonarroti

Reputation: 459

As others already suggested you can chain several statements with the logical AND (&&) operator. Once a statement is false it will return false without checking the other statements. With that in mind, you can return the whole statement if you are only interested if it is true or false

return (name != 'name1' && name != 'name2');

Upvotes: 1

Radu
Radu

Reputation: 540

I'm not too sure what your expected result is, if you want to verify if it's not 'name1' and 'name2' at the same time this should do it

if(name != 'name1' || name != 'name2') return true;

this uses the || (or) operator and returns true if name is either not name1 or either not name2, for example if the variable name is equal to "name1" it will return true.
Another option is

if(name != 'name1' && name != 'name2') return true;

This will return true if name is neither "name1" and "name2" so the variable name must be different from "name1" and "name2" at the same time, you can read more about this here

Upvotes: 0

newbiedude
newbiedude

Reputation: 51

I don't exactly know what you're looking for but maybe you're just mixing up the terms. Variable is your name and the thing to which the variable is compared to is a so called string - here 'name1' or 'name2'.

Try it like this:

if(name !== 'name1' || name !== 'name2') return true;

use || for OR (only one condition needs to be true) use && for AND (both conditions need to be true)

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

You need to check with another variable.

In this case, you need to use a logical AND && operator, because if you use logical OR ||, the condition is always true.

if (name !== 'name1' && name !== 'name2') return true;

Some words to the use of name as variable.

This is not advisable, because window.name is a reserved property of window.

All global variables could be accessed by taking the window object and the name as property.

Upvotes: -1

Related Questions