0

Why is this not working in JavaScript? I'm fairly new to programming.

var material = 'wood';

if (material != ('alu'||'plastic')) {
    material = 'plastic';
}

Thanks in advance!

1
  • 3
    Because || operator in javascript doesn't work like this. Commented Mar 3, 2015 at 15:41

3 Answers 3

4

You could use Array's methods such as indexOf:

if (['alu','plastic'].indexOf(material) >= 0) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

You instead need something like this:

if (material != 'alu' && material != 'plastic')

This is because the || operator requires an expression on both sides, meaning that you need to do a comparison on both sides.

EDIT: I changed it to && because I realized you wanted if check if the material was neither of the two.

Comments

0

Try using regular expression:

var material = 'wood';

if (!/^(.*?(\balu\b|\b plastic\b)[^$]*)$/.test(material)) {
    material = 'plastic';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.