Review the script below. I'm currently testing it on Chrome.
/*create a new set*/
var items = new Set()
/*add an array by declaring its type as an array*/
var arr = [1,2,3,4];
items.add(arr);
/*display items*/
console.log(items); // Set {[1, 2, 3, 4]}
/*add an array directly as an argument*/
items.add([5,6,7,8]);
/*display items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8]}
/*display the type of items stored in Set*/
for (let item of items) console.log(typeof item); //object, object
/*check if the item contains the array we declared as an array*/
console.log(items.has(arr)); // true
/*Now, check if the item has the array added through arguments*/
console.log(items.has([5,6,7,8])); //false
/*Now, add the same array again via argument*/
items.add([1,2,3,4]);
/*Set now contains duplicate items*/
console.log(items); // Set {[1, 2, 3, 4], [5, 6, 7, 8], [1, 2, 3, 4]}
- Why is it returning false at
items.has([5,6,7,8])
? - Why does it allow duplicate values? Isn't "A set is an ordered list of values that cannot contain duplicates"?
- How can I access the array added with
items.add([5,6,7,8])
?