How to check if an array is subset of other array in JavaScript

In this Article we will go through how to check if an array is subset of other array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

// Check if `b` is subset of `a`
const isSubset = (a, b) => (new Set(b)).size === (new Set(b.concat(a))).size;

#Or

const isSubset = (a, b) => b.join("|").includes(a.join("|"));

#Example

isSubset([1, 2], [1, 2, 3, 4]);     // true
isSubset([1, 2, 5], [1, 2, 3, 4]);  // false
isSubset([6], [1, 2, 3, 4]);        // false