Menu
Coddy logo textTech

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:

  1. 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 icon

Challenge

Easy

Create 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
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow