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?

1

8 Answers 8

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

0
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?');​
3
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");    
0
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.

3
  • 1
    You can use this as long as you don't consider IE to be a modern browser: Array.prototype.includes() Browser compatibility. Commented Jul 14, 2017 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. Commented Jul 17, 2017 at 8: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. Commented Jul 17, 2017 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;
}
0
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;
}
2
  • 1
    So long as you don't adhere to Crockford's Good Parts :)
    – alex
    Commented Dec 6, 2012 at 4:48
  • I'm not sure that that's appreciably shorter, though it would be if testing a variable with a longer name.
    – nnnnnn
    Commented Dec 6, 2012 at 4:53
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;
3
  • So long as no one does Object.prototype.jesus = "lol";. You could use Object.create() for an object without this behaviour.
    – alex
    Commented Dec 6, 2012 at 22:43
  • @alex true. That said I wouldn't expect people to directly extend Object.prototype in practice. Am I right?
    – Christophe
    Commented Dec 7, 2012 at 5:10
  • It is rare in practice. But you could swap the in operator for the hasOwnProperty() and then it becomes a non-issue.
    – alex
    Commented Dec 7, 2012 at 5: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?');
}
1
  • You're probably better off just shimming Array.prototoype.indexOf().
    – alex
    Commented Dec 6, 2012 at 4:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.