Concise way to compare against multiple values [duplicate]

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

Leave a Comment