How to return the union of sets in dojox

What is Dojo?

The Dojo is a JavaScript toolkit (library) that provides many utility modules and functions to create a web application. We can save time and speed up the development process using the predefined Dojo modules. Dojo includes language utilities, UI components, and more.

What is a Set ?

A Set is a collection of elements that will not allow duplicate elements.

The dojox/collections/Set module contains utility functions to perform set operations on JavaScript arrays or dojox.collections.ArrayList objects.

How to get the union of two sets?

We can use the union() method of the dojox/collections/Set class to get the unionA set contains all the elements present in set A and set B. The common elements between Set A and Set B will be added once in the resulting set. of two sets.

Syntax

Set.union(setA, setB)
Syntax of union() method

The union() method takes two arguments and returns the union of two sets.

Code example

Let's look at the code below:

Console
Union of Set A and Set B

Code explanation

  • Line 6: We load the Dojo library from the CDN server.

  • Line 8: We load the dojox/collections/Set module. This module contains a utility function to perform set operations.

  • Lines 9–10: After the Set module is loaded, We create two new arrays SetA and SetB with values [1,2,3] and [3,4,5], respectively.

  • Line 13: We use the unoin() method of the Set to get the union of SetA and SetB array. This method returns [1,2,3,4,5] as result.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved