To access items in a Set in JavaScript, you can use methods like forEach to iterate over the set or has to check for item existence.
In this example,
fruitSet
with some initial values.let fruitSet = new Set(['Apple', 'Banana', 'Cherry']);
fruitSet.forEach(function(item) {
console.log(item);
});
Apple Banana Cherry
In this example,
numberSet
with some initial values.let numberSet = new Set([1, 2, 3, 4, 5]);
let containsThree = numberSet.has(3);
console.log('Set contains 3:', containsThree);
Set contains 3: true
In this tutorial, we learned How to Access Items in a Set in JavaScript language with well detailed examples.