• JavaScript Video Tutorials

JavaScript - Set.difference() Method



The JavaScript Set.difference() method takes a set as a parameter and returns a new set containing elements in this set but not in the given set. For instance, we have sets i.e. "set1" and "set2", if we pass set2 as an argument to this method, it returns a new set containing elements in "set1" but not in "set2".

Note −This method got limited browser compatibility. It works only in Safari.

Syntax

Following is the syntax of JavaScript Set.difference() method &mius;

difference(other)

Parameters

This method accepts the following parameter −

  • other − This can be a set object, or set-like object.

Return value

This method returns a set object containing elements in first set but not in the other set.

Examples

Example

In the following example, we are using the JavaScript Set.difference() method to return a new set containing elements in "set1" but not in "set2" −

<html>
<body>
   <script>
      const set1 = new Set([10, 20, 40, 60, 70]);
      const set2 = new Set([10, 40, 90]);
      document.write(odds.difference(result));
   </script>
</body>
</html>

It will return 20, 60, and 70 as result.

Advertisements