23
loading...
This website collects cookies to deliver better user experience
new Set()
let triadSet = new Set([1, 3, 5])
// the Set now looks like this: [1, 3, 5]
let array = [1, 3, 5, 3, 1]
let triadSetFromArray = new Set(array)
// the Set now looks like this: [1, 3, 5]
triadSetFromArray
doesn't repeat the second 3
or 1
because (again), the values in a set are unique!add()
to do so:triadSetFromArray.add(8);
// the Set now looks like this: [1, 3, 5, 8]
add()
method adds the new element to the end of the set object.delete()
method:triadSetFromArray.delete(8);
// the Set now looks like this: [1, 3, 5]
triadSetFromArray
and you want to check what it contains. Sets have a method has()
that you can call to check on the contents. has()
returns a boolean value depending on the contents and works like this:triadSetFromArray.has(5);
// true
triadSetFromArray.has(4);
// false
size
property that you can call to retrieve that kind of data.let array = [1, 3, 5, 3, 1]
let triadSetFromArray = new Set(array)
return triadSetFromArray.size
// 3
clear()
method to do so:triadSetFromArray.clear();
// The Set now looks like this: []