Skip to content

Latest commit

 

History

History
68 lines (58 loc) · 1.5 KB

File metadata and controls

68 lines (58 loc) · 1.5 KB

Sets

Sets represents a collections of unique values- No duplication.

Set Properties

  • unique/no duplicates
  • no indexing- elements cannot be acccessed by index
  • elements in set don't maintain their order.

Sets are created using new Set() -returns number of elements and the set itself.

const cordinates = new Set([1,2,3,4,5,6]);
const mixedSet = new Set ([10,5,"Gideon"]);

console.log(cordinates);
console.log(mixedSet);
output:
Set(6) { 1, 2, 3, 4, 5, 6 }
Set(3) { 10, 5, 'Gideon' }

Sets Methods

  1. add() - adds new element
const mixedSet = new Set ([10,5,"Gideon"]);
mixedSet.add("Pascal")
mixedSet.add("Levi")
mixedSet.add(80)
mixedSet.add(27)

console.log(mixedSet);
output:
Set(7) { 10, 5, 'Gideon', 'Pascal', 'Levi', 80, 27 }
  1. delete() - removes specified element
const mixedSet = new Set ([10, 5, 'Gideon', 'Pascal', 'Levi', 80, 27]);
mixedSet.delete("Pascal")
mixedSet.delete("Levi")
mixedSet.delete(80)
mixedSet.delete(27)

console.log(mixedSet);
output:
Set(3) { 10, 5, 'Gideon' }
  1. has() - returns true if specified value exist in a set.
const mixedSet = new Set ([10, 5, 'Gideon', 'Pascal', 'Levi', 80, 27]);

console.log(mixedSet.has("Levi"));
console.log(mixedSet.has("Naomi"));
console.log(mixedSet.has(34));
output:
true
false
false
  1. size() - returns the number of elements in a set.
const mixedSet = new Set ([10, 5, 'Gideon', 'Pascal', 'Levi', 80, 27]);

console.log(mixedSet.size);
output
7