Javascript Sets

JavaScript Sets

ES6 has given us Sets, a list type that is kinda like arrays, but better, mainly because Sets automatically don't allow duplicates, so list management just got a whole lot easier.

USAGE: Copy and paste the code samples below into the Javascript console in your browser, then simply hit return on your keyboard to see the code in action.

//A list of unique values (arrays can contain duplicate values, sets only contain unique values 
//(easier to manage then arrays))
let set = new Set([1, 1, 1]);
console.log("New set: " + set);
console.log('does set contain 15? ' + set.has(15));

for (element of set) {

//Sets (sorta like CF structs)
let names = new Set();
console.log("size of this set is:" + " " + names.size);
console.log("size of this set is:" + " " + names.size);
console.log("size of this set is:" + " " + names.size);

//More sets (duplicates)
let bros = ["Mike", "Rich", "Bob", "Dave", "Dave", "Mike"];
let refinedBros = new Set(bros);
console.log('refinedBros: ' +refinedBros);
//the spread operator adds elements into an array
bros = [...refinedBros];
console.log("Final list: " + bros);

Link your website to this page! Copy and paste the URL below:
Copyright 2021. All Rights Reserved.