Back to TIL list

javascript set

Created at

https://alligator.io/js/sets-introduction/

let animals = new Set();animals.add('🐷');animals.add('🐼');animals.add('🐢');animals.add('🐿');console.log(animals.size); // 4animals.add('🐼');console.log(animals.size); // 4console.log(animals.has('🐷')); // trueanimals.delete('🐷');console.log(animals.has('🐷')); // falseanimals.forEach(animal => {  console.log(\`Hey ${animal}!\`);});// Hey 🐼!// Hey 🐢!// Hey 🐿!animals.clear();console.log(animals.size); // 0
let myAnimals = new Set(['🐷', '🐢', '🐷', '🐷']);myAnimals.add(['🐨', '🐑']);myAnimals.add({ name: 'Rud', type: '🐢' });console.log(myAnimals.size); // 4myAnimals.forEach(animal => {  console.log( animal );});// 🐷// 🐢// ["🐨", "🐑"]// Object { name: "Rud", type: "🐢" }