set contains javascript code example

Example 1: check if set has value js

const mySet = new Set();

// Add value to set
mySet.add(15);

console.log(mySet.has(33)) // expected output: false
cosole.log(mySet.has(15)) // expected output: true

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
}

Example 3: set.contains in javascript

const set = new Set();

set.add(1);
set.add(2);
set.add(3);

console.log(set.has(2));  //true
console.log(set.has(4)); //false

Tags:

Html Example