Adding and deleting items from a Set
We will start out with the simplest possible tasks involving a Set. In this recipe, we'll take a look at how to add and delete items from a Set using the respective instance methods.
Getting ready
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
How to do it...
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
12-01-add-remove-from-set. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js. - Create a
main.jsfile that defines amainfunction. In that function, create a newSetinstance, then add and remove a few items from it:
// main.js
export function main() {
const rocketSet = new Set();
rocketSet.add('US: Saturn V');
rocketSet.add('US: Saturn V');
rocketSet.add('US: Falcon Heavy');
console.log(rocketSet);
rocketSet.delete('US: Falcon Heavy');
console...