Javascript: Simple Way to Check If Variable Is Equal to One of Two or More Values

JavaScript: Simple way to check if variable is equal to one of two or more values?

You can stash your values inside an array and check whether the variable exists in the array by using [].indexOf:

if([5, 6].indexOf(x) > -1) {
// ...
}

If -1 is returned then the variable doesn't exist in the array.

Check variable equality against a list of values

Using the answers provided, I ended up with the following:

Object.prototype.in = function() {
for(var i=0; i<arguments.length; i++)
if(arguments[i] == this) return true;
return false;
}

It can be called like:

if(foo.in(1, 3, 12)) {
// ...
}

Edit: I came across this 'trick' lately which is useful if the values are strings and do not contain special characters. For special characters is becomes ugly due to escaping and is also more error-prone due to that.

/foo|bar|something/.test(str);

To be more precise, this will check the exact string, but then again is more complicated for a simple equality test:

/^(foo|bar|something)$/.test(str);

What's the prettiest way to compare one value against multiple values?

Don't try to be too sneaky, especially when it needlessly affects performance.
If you really have a whole heap of comparisons to do, just format it nicely.

if (foobar === foo ||
foobar === bar ||
foobar === baz ||
foobar === pew) {
//do something
}

How do I test if a variable does not equal either of two values?

Think of ! (negation operator) as "not", || (boolean-or operator) as "or" and && (boolean-and operator) as "and". See Operators and Operator Precedence.

Thus:

if(!(a || b)) {
// means neither a nor b
}

However, using De Morgan's Law, it could be written as:

if(!a && !b) {
// is not a and is not b
}

a and b above can be any expression (such as test == 'B' or whatever it needs to be).

Once again, if test == 'A' and test == 'B', are the expressions, note the expansion of the 1st form:

// if(!(a || b)) 
if(!((test == 'A') || (test == 'B')))
// or more simply, removing the inner parenthesis as
// || and && have a lower precedence than comparison and negation operators
if(!(test == 'A' || test == 'B'))
// and using DeMorgan's, we can turn this into
// this is the same as substituting into if(!a && !b)
if(!(test == 'A') && !(test == 'B'))
// and this can be simplified as !(x == y) is the same as (x != y)
if(test != 'A' && test != 'B')

How do I test if a JS variable does equal either of two values?

I thought your original post was pretty straight forward and thought it was a perfectly fine answer to your problem.

Having said that you can use RegEx like @le_m mentioned. I just tested this in the console and this works.

The '//' tell the interpreter that we will be starting a regular expression. The parenthesis allow us to declare a subexpression. A subexpression that will be evaluated independently of other expressions.

The 'i' stands for insensitive. In this case it isn't entirely necessary but I think it might be useful for you down the road. The 'i' will allow you to avoid casing issues. Also, should you pass in longer strings that contain 'display', or 'type' in a sentence, this regex will also find those.

var type = 'display';
if (/(type|display)/i.test(type)) {
console.log('word found')
}

Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands

You could use a variable and multiple comparison, but that's still lengthy:

var text = item[i].innerText;
if (text === 'Argentina' || text === 'Australia' || text === 'Brazil' || text === 'Canada' || text === 'China' || text === 'Colombia' || text === 'France' || text === 'Germany' || text === 'Indonesia' || text === 'India' || text === 'Italy' || text === 'Japan' || text === 'Malaysia' || text === 'Mexico' || text === 'Philippines' || text === 'Russia' || text === 'South Africa' || text === 'Sweden' || text === 'Switzerland' || text === 'United Kingdom' || text === 'USA')

Or you could just use an array and check if the string is contained in it.

var matches = ['Argentina','Australia','Brazil','Canada','China','Colombia','France','Germany','Indonesia','India','Italy','Japan','Malaysia','Mexico','Philippines','Russia','South Africa','Sweden','Switzerland','United Kingdom','USA'];
if (~ matches.indexOf(item[i].innerText) …

Yet, for the complicated != -1 comparison and the lack of native indexOf in older IEs, people tend to use regexes:

var regex = /Argentina|Australia|Brazil|Canada|China|Colombia|France|Germany|Indonesia|India|Italy|Japan|Malaysia|Mexico|Philippines|Russia|South Africa|Sweden|Switzerland|United Kingdom|USA/
if (regex.test(item[i].innerText)) …

shorthand for conditions like (x === y) || (x === z)?

You can use includes() like so:

if ([y, z].includes(x)) { ... }
x = [y, z].includes(x) ? ... : ...;

As noted in the comments, this will not work with IE11 and older browsers.



Related Topics



Leave a reply



Submit