Javascript Set Reference Type

Introduction

The Set object can store unique values of any type.

Using the Set object

let mySet = new Set();

mySet.add(1);//from   www . j av a2 s. co  m
mySet.add(5);
mySet.add(5);
console.log(mySet); //duplicate removed
mySet.add('some text');
let o = {a: 1, b: 2}
mySet.add(o);
console.log(mySet); 
mySet.add({a: 1, b: 2})
console.log(mySet); 

console.log(mySet.has(1));           
console.log(mySet.has(3));
console.log(mySet.has(5));
console.log(mySet.has('Some Text'.toLowerCase()));
console.log(mySet.has(o));

console.log(mySet.size);

console.log(mySet);
mySet.delete(5);    // removes 5 from the set
console.log(mySet);



PreviousNext

Related