set method for array js 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: set methods in js

let chars = new Set(['a', 'a', 'b', 'c', 'c']);Code language: JavaScript (javascript)