Common problems when comparing values using JavaScript

One of the core principles of JavaScript is if/else statements to compare values, however there are some easily-made mistakes when using these statements which you need to be aware of.

Example

A common mistake when using comparisons is to not specify logic for each item you are trying to compare.

if ([Company] == 'Company1' || 'Company2' || 'Company3') {
// Do something if the company name matches any of the above.
} else {
// Do this for any other company.
}

What is expected in the code above is for any company with the name "Company1", "Company2" or "Company3" to run the first part of the code. However, in its current state it will run the first part of the code for any company.

While it is understandable to humans to say the following:

If this word equals "one" or "two" or "three"

Computers will interpret the above incorrectly. Computers require specification for each word it is comparing, for example:

If this word equals "one", or if this word equals "two", or if this word equals "three"

An example of how it should be written can be found below:

if ([Company] == 'Company1' || [Company] == 'Company2' || [Company] == 'Company3') {
// Do something if the company name matches any of the above.
} else {
// Do this for any other company.
}