Phase to add data
Add data to Set on Initialisation
Below show the illustration on initialise set with data. It is simply having an array to hold onto a list of values. We can initialise a Set with values of different datatype at the same time.
After initialisation, we will be able to get the results.
const mySet = new Set(['Person1', 101]); console.log(mySet.has('Person1')); // true console.log(mySet.has(101)); // true console.log(mySet.has('101')); // false
Add data to Set during Runtime
If there is a need to insert new value in the middle of the code, we can utilise
add() method to tell Set to include new values into its dataset one at a time. In top of that, values can also be of different datatype.
const nameSet = new Set(); nameSet.add('Person1'); nameSet.add(101); console.log(nameSet.has('Person1')); // true console.log(nameSet.has('Person2')); // false console.log(mySet.has(101)); // true console.log(mySet.has('101')); // false
add() method to insert new values.
For other examples, can refer to MDN documentation here.