JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
52: The Set Object
Wait, why do I need this if I already have Arrays?
I'll be honest: at first glance, a Set looks like a stripped-down Array. It holds a list of values, you can add to it, and you can iterate over it. But there is one massive difference: a Set cannot contain duplicate values. If you try to add the same string or number twice, the Set simply ignores the second attempt.
Beyond just uniqueness, there's a performance win here. If you have a list of 10,000 items and you want to check if a specific ID exists, array.includes() has to potentially look at every single element until it finds a match. A Set uses a hash table under the hood, meaning set.has() is nearly instantaneous regardless of how large the set is. If you're doing frequent lookups, stop using arrays and switch to a Set.
Is there a quick way to use this to clean up a messy array?
This is actually the most common way I use Sets in my daily work. Let's say you're pulling tags from a database for a blog post, and for some reason, the data is dirty and contains duplicates.
const rawTags = ['javascript', 'webdev', 'javascript', 'frontend', 'webdev', 'coding'];
// The "Magic" One-Liner
const uniqueTags = [...new Set(rawTags)];
console.log(uniqueTags); // ['javascript', 'webdev', 'frontend', 'coding']
Here's what's happening: I pass the array into new Set(), which immediately strips out the duplicates. But since that returns a Set object and not an array, I wrap the whole thing in brackets and use the spread operator (...) to "spread" those unique values back into a fresh array. It's clean, fast, and far better than writing a forEach loop with an if statement inside it.
How do I actually manage the data inside a Set?
Managing a Set is slightly different from an Array. You don't have indexes (you can't do mySet[0]), because the order isn't the primary focus—the uniqueness is. Instead, you use a few specific methods:
.add(value): Adds a new element. You can chain these together since.add()returns the set itself..has(value): Returns a boolean. This is your replacement for.includes()..delete(value): Removes a specific element..clear(): Wipes the entire thing..size: A property (not a function!) that tells you how many items are inside.
const activeUsers = new Set();
activeUsers.add('user_123').add('user_456').add('user_123');
console.log(activeUsers.size); // 2 (because user_123 was added twice)
if (activeUsers.has('user_456')) {
console.log('User is online!');
}
activeUsers.delete('user_123');
📋 Practical Task
Exercise: Guest List De-duplicator
You are building a check-in system for a tech conference. Due to a bug in the registration form, some guests have signed up multiple times using the same email address. You have been given an array of emails, but your badge-printing software will crash if it receives duplicate entries.
Your Task:
- Create an array called
rawGuestListcontaining the following emails:'alice@example.com','bob@example.com','alice@example.com','charlie@example.com', and'bob@example.com'. - Use a
Setto remove the duplicates from this array. - Convert that Set back into an array called
cleanedGuestList. - Log the
cleanedGuestListto the console and log the total number of unique guests using the.lengthproperty of the resulting array.
There are no comments for now.