Coding Newbie

Get Unique Values From An Array In JavaScript

To get unique values from an array in JavaScript is pretty simple and straightforward process.

In order to better understand what arrays are and how do arrays behave, please read the article Learn JavaScript Arrays Quickly And Easily.

How To Get Unique Value From An Array

To get a only unique values from an array we need to use Set() constructor. What is Set() constructor?

The Set constructor lets you create Set objects that store unique values of any type

Line of code to get unique value items is:

const uniqueItems = […new Set(array)];

Or in example:

const array = [1,5,2,5,2,3,1,4,4,2,"a","b","a","c","a"];
const uniqueItems = [ Set(array)];


The result is a new array compiled only of unique values:

unique array items javascript

We used const as a variable declaration. If you are not familiar with it, or you are but want to know more about it, please read the post Introducing CONST: New JavaScript Declaration.

In this code we used so-called spread operator (the three dots). In this case, spread operator helps us to create an array after we used Set() constructor.


In this article we have learned what line of code we can use to get unique value items from an array and store it as a new array.

We create a new object consisted of unique items with the use of Set() operator. Than we take those items and with little help of spread operator ( … ) we create new array.

%d bloggers like this: