Categories
JavaScript Answers

How to create a set in JavaScript?

Spread the love

Sometimes, we want to create a set in JavaScript.

In this article, we’ll look at how to create a set in JavaScript.

How to create a set in JavaScript?

To create a set in JavaScript, we can use the Set constructor.

For instance, we write

const set = new Set();
set.add("red");
const hasRed = set.has("red");
set.delete("red");

to create a new set with the Set constructor.

Then we call add to add 'red' into the set.

Next we call has to check if 'red' is in the set.

And then we call delete to remove 'red' from the set.

Conclusion

To create a set in JavaScript, we can use the Set constructor.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *