How to get all unique values in a JavaScript array?

By Gulshan Saini
Published in JavaScript
October 27, 2020
1 min read

Let’s say you have an array of numbers, which has duplicate items and, you want to clean it up so that it only holds unique value. To remove duplicate items, we can use the Set data structure. The Set object allows you to create a collection of unique values and does not permit the insertion of duplicate items.

Let’s look at the following example

let array = [1, 1, 3, 4, 3, 5, 6, 7, 8, 9, 0, 11, 12, 3]

The array variable holds a lot of duplicate items and only 11 unique items. To filter out duplicate items, we need to pass the array variable as input to the Set constructor.

let array = [1, 1, 3, 4, 3, 5, 6, 7, 8, 9, 0, 11, 12, 3]

let unique = new Set(array)

That’s it, if we now log the value of the unique variable we can see it now holds unique values only.


console.log(unique) // 1 3 4 5 6 7 8 9 0 11 12

Learn how to convert set to array again.


Previous Article
How to Get Current Date in Human Readable Form in JavaScript?

Related Posts

How to check if a string contains a substring in JavaScript?
January 05, 2021
1 min
Gulshan Saini

Gulshan Saini

Fullstack Developer



Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.
© 2021, All Rights Reserved.

Quick Links

Contact UsBrowserCSSPythonPuppeteer

Social Media