What Is A Set?
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 28 of 65.
A Set is a built-in object in JavaScript that stores unique values of any type. It's similar to an array, but with one key difference:
- Each value in a Set must be unique
Here's how to create a Set:
const mySet = new Set();You can also initialize a Set with an array:
const mySet = new Set([1, 2, 3, 4, 5]);To convert a set back to array:
const myArr = Array.from(mySet);Challenge
EasyCreate a function called uniqueElements that takes an array as an argument. The function should return a new array containing only the unique elements from the input array.
Cheat sheet
A Set is a built-in JavaScript object that stores unique values of any type. Each value in a Set must be unique.
Create a Set:
const mySet = new Set();Initialize a Set with an array:
const mySet = new Set([1, 2, 3, 4, 5]);Convert a Set back to an array:
const myArr = Array.from(mySet);Try it yourself
function uniqueElements(arr) {
// Write your code here
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys