41

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?

alex
  • 479,566
  • 201
  • 878
  • 984
FatDogMark
  • 1,207
  • 2
  • 16
  • 25

8 Answers8

60

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().

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
alex
  • 479,566
  • 201
  • 878
  • 984
31

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?');​
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • 3
    And contrary to array.indexOf my solution works for browsers BC (Before Chrome) – Christophe Dec 06 '12 at 15:54
  • 2
    regex is 5 times slower see the test https://runkit.com/pramendra/58cad911146c1c00147f8d8d – Pramendra Gupta Mar 16 '17 at 18:44
  • Use [`test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) instead of [`exec()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) for faster execution. `test` will return only true or false, `exec` returns an array of results. – Joshua Fan Nov 28 '19 at 20:56
18

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");    
Adil
  • 146,340
  • 25
  • 209
  • 204
10

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.

Maciej Bukowski
  • 3,240
  • 23
  • 28
  • 1
    You can use this as long as you don't consider IE to be a modern browser: [Array.prototype.includes() Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Browser_compatibility). – Sumner Evans Jul 14 '17 at 16:07
  • 1
    IE is already 4-year-old. And like I said, there's a polyfill for that and you can even use Babel for creating builds for some older platforms. – Maciej Bukowski Jul 17 '17 at 08:44
  • 2
    Understood. My point was a joke about IE. I upvoted this since it is the best and most current answer for modern JS. – Sumner Evans Jul 17 '17 at 18:50
8

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;
}
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
4

May be using switch instead of if:

var a = 'jesus';
switch (a){
    case 'something':
    case 'nothing' :
    case 'anything':
    case 'everything':
   alert('Who cares?');
   break;
}
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
2

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;
alex
  • 479,566
  • 201
  • 878
  • 984
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • So long as no one does `Object.prototype.jesus = "lol";`. You could use `Object.create()` for an object without this behaviour. – alex Dec 06 '12 at 22:43
  • @alex true. That said I wouldn't expect people to directly extend Object.prototype in practice. Am I right? – Christophe Dec 07 '12 at 05:10
  • It is rare in practice. But you could swap the `in` operator for the `hasOwnProperty()` and then it becomes a non-issue. – alex Dec 07 '12 at 05:15
0

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?');
}
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50