In JavaScript, a set is a way to store a collection of unique values. It’s like having a box where you can put things, but each thing can only be put in once. Sets can be created using the Set() constructor, and you can add and remove values using the add() and delete() methods, respectively.
For example, let’s say you want to create a set of your favorite fruits:
// create a set of fruits const favoriteFruits = new Set(); // add some fruits to the set favoriteFruits.add('apple'); favoriteFruits.add('banana'); favoriteFruits.add('orange'); // remove a fruit from the set favoriteFruits.delete('banana'); // check if a fruit is in the set if (favoriteFruits.has('apple')) { console.log('I love apples!'); } // get the number of fruits in the set console.log(`I have ${favoriteFruits.size} favorite fruits.`);
So, sets are a useful tool in JavaScript for storing collections of unique values, and they can be manipulated using methods like add(), delete(), has(), and size.