Javascript - Set Operations (add, delete, has)
Code Life
By Pierre Marsaa
Adding Values to a Set
Imagine updating your Codédex as you explore: every time you catch a Codémon, you want to register its name—without ever creating duplicates if you catch the same species twice.
To insert a value into a Set, use the .add(value) method. If the value is already inside the Set, .add() simply does nothing:
↗️
Loading editor...
↶
↷
Even though .add() was called 3 times, codedex only contains 2 entries ("Zap" and "Aquachu"). You never need to write an if check before adding an item—the Set prevents duplicates for you!
Verify