Concise way to compare against multiple values [duplicate]

Consider the following:

var a = 'jesus';

if(a == 'something' || a == 'nothing' || a=='anything' || a=='everything'){
   alert('Who cares?');
}

Is there a way to make this shorter?

Is there anything in Javascript like if (a=='bbb'||'ccc')?

In addition, can jQuery help here?

Answers:

Answer

With a regex:

if (/^(something|nothing|anything|everything)$/.exec('jesus')) alert('Who cares?');?

Or the opposite:

/^(something|nothing|anything|everything)$/.exec('jesus')||alert('Who cares?');?

[Update] Even shorter ;-)

if (/^(some|no|any|every)thing$/.exec('jesus')) alert('Who cares?');?
Answer

You can put the options in array and use jQuery $.inArray() or javascrpt indexOf() to search array

Pure javascript  

Live Demo

var a = 'anything';
arr = ['something', 'nothing', 'anything', 'everything'];
if(arr.indexOf(a) != -1)
    alert("condition met");    
else
    alert("condition not met");    

With jQuery

Live Demo

var a = 'jesus';
arr = ['something', 'nothing', 'anything', 'everything'];

if($.inArray(a, arr) != -1) // With jQuery
    alert("condition met");    
else
    alert("condition not met");    
Answer

Try this:

If you want to check the words other than Jesus, try following,

if(a != "jesus"){
   alert('Who cares?');
}

if you want to check particular words, try following,

var check_arrays = ['something','nothing', 'anything', 'everything'];
if(checkThis(a)){
   alert('Who cares?');
}

function checkThis(a)
{
   for(i=0;i<check_arrays.length;i++)
   {
      if(check_arrays[i] == a)
      return true;
   }
   return false;
}
Answer

With ES7 you are able to use Array.prototype.includes and even shorter notation than indexOf, which is already implemented in every modern browser.

Here is the example usage:

if (['hero', 'anything', 'everything'].includes(me)) {
    alert('Who cares?');
}

And the polyfill from Mozilla.

Answer

May be using switch instead of if:

var a = 'jesus';
switch (a){
    case 'something':
    case 'nothing' :
    case 'anything':
    case 'everything':
   alert('Who cares?');
   break;
}
Answer

Using an object literal:

var all={something:1,nothing:1,anything:1,everything:1};
if (all.hasOwnProperty('jesus')) alert('Who cares?');?

I'd say this is the most concise cross-browser expression for general use (well, as long as the objective is to compare strings). It is also very flexible as you can easily add or remove properties:

all.mything=1;
delete all.nothing;
Answer

I think there is no easy method to do this. But you can write a function in_array and you can use it anywhere you need

Array.prototype.in_array = function(p_val) {
    for(var i = 0, l = this.length; i < l; i++) {
        if(this[i] == p_val) {
            return true;
        }
    }
    return false;
}

var v_array = [ 'something','nothing', 'anything', 'everything'];

var a = "jesus";

if(v_array.in_array(a)){
    alert('Who cares?');
}
Answer

You could use this...

if (["something", "nothing", "anything", "everything"].includes(a)) {
   alert('Who cares?');
}

If you're stuck with older browser support...

if (["something", "nothing", "anything", "everything"].indexOf(a) > -1) {
   alert('Who cares?');
}

You also tagged it jQuery, so if you need to support older browsers without Array.prototype.indexOf(), you could use $.inArray().

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.