get first from set javascript code example
Example 1: set in javascript
// set is used for storing unique values
const firstSet = new Set([1, 2, 3]);
firstSet.add('hi'); //adding value to set
firstSet.add(3); //this will not give any error and it will also not be added
firstSet.delete('hi');//removing value from set
console.log(firstSet.has('hi'));//checking 'hi' is in the set or not
// showing all values in the set
console.log(firstSet);
for (const entry of firstSet.values()) {
console.log(entry);
Example 2: add to set js
const mySet = new Set();
mySet.add(15);
mySet.add(33);
mySet.add(15);
for(let item of mySet) {
console.log(item)
// expected output: 15
// expected output: 33
}