JavaScript Set Reference

Complete reference guide for Set methods and properties

📚 Set Reference Guide

This is a complete reference for all Set methods, properties, and features in JavaScript. Use this as a quick lookup guide.


// Set constructor
const mySet = new Set();
const mySetWithValues = new Set([1, 2, 3]);
                                    

Set Constructor

Syntax:

new Set([iterable])
// Empty Set
const set1 = new Set();

// Set from array
const set2 = new Set([1, 2, 3, 4]);

// Set from string
const set3 = new Set("hello");

// Set from another Set
const set4 = new Set(set2);

console.log("Empty:", set1);
console.log("From array:", set2);
console.log("From string:", set3);
console.log("From Set:", set4);

Output:

Empty: Set(0) {}
From array: Set(4) {1, 2, 3, 4}
From string: Set(4) {'h', 'e', 'l', 'o'}
From Set: Set(4) {1, 2, 3, 4}

🔹 Set Properties

📏

size

Returns the number of elements

const mySet = new Set([1, 2, 3, 4, 5]);

console.log("Set size:", mySet.size);

// Size changes when adding/removing
mySet.add(6);
console.log("After adding:", mySet.size);

mySet.delete(1);
console.log("After deleting:", mySet.size);

Output:

Set size: 5
After adding: 6
After deleting: 5

🔹 Set Methods

🔸 Modification Methods

  • add(value): Adds a value to the Set
  • delete(value): Removes a value from the Set
  • clear(): Removes all values from the Set
const mySet = new Set();

// add() - returns the Set object
mySet.add(1).add(2).add(3);  // Method chaining
console.log("After adding:", mySet);

// delete() - returns true if deleted, false if not found
console.log("Delete 2:", mySet.delete(2));
console.log("Delete 5:", mySet.delete(5));

// clear() - removes all elements
mySet.clear();
console.log("After clear:", mySet);

Output:

After adding: Set(3) {1, 2, 3}
Delete 2: true
Delete 5: false
After clear: Set(0) {}

🔸 Query Methods

  • has(value): Returns true if value exists in Set
const fruits = new Set(['apple', 'banana', 'orange']);

console.log("Has apple:", fruits.has('apple'));
console.log("Has grape:", fruits.has('grape'));

// Case sensitive
console.log("Has Apple:", fruits.has('Apple'));

Output:

Has apple: true
Has grape: false
Has Apple: false

🔸 Iteration Methods

  • values(): Returns iterator for values
  • keys(): Same as values() (for compatibility)
  • entries(): Returns iterator for [value, value] pairs
  • forEach(): Executes function for each value
const numbers = new Set([1, 2, 3]);

// values() iterator
console.log("Values:");
for (let value of numbers.values()) {
    console.log(value);
}

// entries() iterator
console.log("Entries:");
for (let [key, value] of numbers.entries()) {
    console.log(key, value);  // key === value in Sets
}

// forEach method
console.log("forEach:");
numbers.forEach((value, value2, set) => {
    console.log(`Value: ${value}`);
});

Output:

Values:
1
2
3
Entries:
1 1
2 2
3 3
forEach:
Value: 1
Value: 2
Value: 3

🔹 Set Static Methods

Available Static Methods:

  • Set[Symbol.species]: Constructor function used to create derived objects
// Set is iterable by default
const mySet = new Set([1, 2, 3]);

// Can be used with spread operator
const array = [...mySet];
console.log("Set to array:", array);

// Can be used with Array.from()
const array2 = Array.from(mySet);
console.log("Array.from Set:", array2);

Output:

Set to array: [1, 2, 3]
Array.from Set: [1, 2, 3]

🔹 Common Set Patterns

// Remove duplicates from array
const arrayWithDuplicates = [1, 2, 2, 3, 3, 4];
const uniqueArray = [...new Set(arrayWithDuplicates)];
console.log("Unique array:", uniqueArray);

// Check if arrays have same elements
const arr1 = [1, 2, 3];
const arr2 = [3, 2, 1];
const set1 = new Set(arr1);
const set2 = new Set(arr2);
const sameElements = set1.size === set2.size && 
                    [...set1].every(x => set2.has(x));
console.log("Same elements:", sameElements);

// Convert Set to JSON
const mySet = new Set([1, 2, 3]);
const jsonString = JSON.stringify([...mySet]);
console.log("Set as JSON:", jsonString);

Output:

Unique array: [1, 2, 3, 4]
Same elements: true
Set as JSON: [1,2,3]

🔹 Quick Reference Table

Method Description Returns
add(value) Adds a value Set object
delete(value) Removes a value boolean
has(value) Checks if value exists boolean
clear() Removes all values undefined
forEach() Executes function for each value undefined

🧠 Test Your Knowledge

Which method returns the Set object itself?