How to insert an element in a set with JavaScript

1 Answer

0 votes
// Create a new Set
const mySet = new Set();

// Add elements to the Set
mySet.add(1);
mySet.add(2);
mySet.add(3);
mySet.add(4);

// Attempt to add a duplicate element
mySet.add(2); // No effect, as 2 is already in the Set

console.log(mySet);


/*
run:

Set(4) { 1, 2, 3, 4 }

*/

 



answered Aug 5 by avibootz
...