How to check whether multiple values exist within an Javascript array

Native JavaScript solution

var success = array_a.every(function(val) {
    return array_b.indexOf(val) !== -1;

You’ll need compatibility patches for every and indexOf if you’re supporting older browsers, including IE8.

  • Compatibility patch from MDN for .every().
  • Compatibility patch from MDN for .indexOf().

Full jQuery solution

var success = $.grep(array_a, function(v,i) {
    return $.inArray(v, array_b) !== -1;
}).length === array_a.length;

Uses $.grep with $.inArray.

ES2015 Solution

The native solution above can be shortened using ES2015’s arrow function syntax and its .includes() method:

let success = array_a.every((val) => array_b.includes(val))

Leave a Comment