40

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

Sign up to request clarification or add additional context in comments.

Comments

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 Comments

And contrary to array.indexOf my solution works for browsers BC (Before Chrome)
regex is 5 times slower see the test runkit.com/pramendra/58cad911146c1c00147f8d8d
Use test() instead of exec() for faster execution. test will return only true or false, exec returns an array of results.
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");    

Comments

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 Comments

You can use this as long as you don't consider IE to be a modern browser: Array.prototype.includes() Browser compatibility.
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.
Understood. My point was a joke about IE. I upvoted this since it is the best and most current answer for modern JS.
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;
}

Comments

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 Comments

So long as you don't adhere to Crockford's Good Parts :)
I'm not sure that that's appreciably shorter, though it would be if testing a variable with a longer name.
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 Comments

So long as no one does Object.prototype.jesus = "lol";. You could use Object.create() for an object without this behaviour.
@alex true. That said I wouldn't expect people to directly extend Object.prototype in practice. Am I right?
It is rare in practice. But you could swap the in operator for the hasOwnProperty() and then it becomes a non-issue.
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 Comment

You're probably better off just shimming Array.prototoype.indexOf().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.