How to create an array of unique values in JavaScript

April 1st, 20236 mins read

How to create an array of unique values in JavaScript

As a programmer, you know that arrays are a powerful tool in JavaScript. However, sometimes you need to ensure that the data in your array is unique.

Why? Imagine if you had a list of names, and some of them were repeated. It would make it difficult to find a specific name, wouldn't it? That's why having an array of unique values is crucial.

In this article, you’ll learn how to create an array of unique values in JavaScript. This article will cover three methods that are easy to implement and will save you time and headaches in the long run.

So let's dive in and get started!

What Is an Array?

Arrays are a powerful feature of JavaScript that allows you to store and manipulate data. An array is simply a list of items, where each item can be accessed using an index number (JavaScript arrays are zero-indexed).

In JavaScript, an array is defined using square brackets, and commas separate items in the array. For example, an array of numbers might look like this:

const numbers = [1, 2, 3, 4, 5];

Arrays can hold any data type, including strings, objects, and other arrays. Using various built-in methods, you can add, remove, or modify items in an array.

Arrays are used in many different ways in JavaScript. For example, they can store user input, manipulate and display data in a web application, or even create dynamic animations. The possibilities are endless!

One of the most powerful aspects of arrays is their ability to be iterated using loops or higher-order functions like map() or reduce(). This makes it easy to perform complex operations on large data sets with just a few lines of code.

What Are Unique Values?

Now that we know what arrays are let's talk about unique values. In JavaScript, a unique value is simply a value that appears only once in a given dataset.

For example, let's say we have an array of colors:

const colors = ['red', 'blue', 'green', 'yellow', 'red', 'blue'];

In this case, the values 'red' and 'blue' appear twice, while 'green' and 'yellow' appear only once. To create an array of unique values from this dataset, we would need to remove the duplicate 'red' and 'blue' values.

Arrays with non-unique values can cause programming problems, leading to incorrect calculations or unexpected behavior. That's why it's important to identify and remove duplicate values whenever possible.

Luckily, JavaScript provides several methods for creating an array of unique values. We'll cover three of the most commonly used methods in this article.

Methods To Create an Array of Unique Values in JavaScript

Now that you understand what arrays and unique values are and why they're important, let's explore some methods for creating an array of unique values in JavaScript.

Method 1: Using the Set Object

The Set object is a built-in object in JavaScript that allows you to create a collection of unique values.

Duplicates are automatically removed when you add values to a Set object.

To create an array of unique values using a Set object, we can simply pass the original array into the Set constructor and then convert the Set back into an array using the spread operator. Here's an example:

const originalArray = ['apple', 'banana', 'cherry', 'apple', 'banana'];

const uniqueArray = [...new Set(originalArray)];

console.log(uniqueArray); // returns ['apple', 'banana', 'cherry']

Method 2: Using the filter() and indexOf() Methods

The filter() method creates a new array with all elements that pass a certain test. Combining it with the indexOf() method can create an array of unique values. The indexOf() method returns the first index at which a given element can be found in the array.

Here's an example of how to use the filter() and indexOf() methods to create an array of unique values:

const originalArray = ['apple', 'banana', 'cherry', 'apple', 'banana'];

const uniqueArray = originalArray.filter((value, index, self) => {
	return self.indexOf(value) === index;
});

console.log(uniqueArray); // returns ['apple', 'banana', 'cherry']

Method 3: Using the reduce() and includes() Methods

The reduce() method reduces an array to a single value by iterating over each element and applying a function. You can use the reduce() method to create an array of unique values by checking whether the array already includes the current value.

Here's an example of how to use the reduce() and includes() methods to create an array of unique values:

const originalArray = ['apple', 'banana', 'cherry', 'apple', 'banana'];

const uniqueArray = originalArray.reduce((accumulator, currentValue) => {
	if (!accumulator.includes(currentValue)) {
		accumulator.push(currentValue);
	}
	return accumulator;
}, []);

console.log(uniqueArray); // returns ['apple', 'banana', 'cherry']

These three methods provide different approaches to creating various unique values in JavaScript. Depending on your specific use case, one method may be more appropriate.

Try them out and see which one works best for you!

Conclusion

In this article, you have explored three methods for creating an array of unique values in JavaScript.

The first method involves using the Set object, which automatically removes duplicates when you pass an array into it. The second method uses the filter() and indexOf() methods to create a new array with only the unique values from the original array. Finally, the third method uses the reduce() and includes() methods to create a new array of unique values by checking whether the array already includes the current value.

In programming, unique values are important for various reasons, such as ensuring the accuracy of data and avoiding errors that can occur from duplicates. By using these methods to create an array of unique values in JavaScript, you can improve the performance and reliability of your code.

It's important to note that there are often multiple ways to solve a programming problem, and it's up to you to choose the best method for your specific use case. Experiment with these methods and see which one works best for you.

In summary, creating an array of unique values in JavaScript is a common task that can be achieved using various methods. By understanding the importance of unique values in programming and exploring these methods, you can improve the effectiveness and efficiency of your code.

Try what you have learned in the interactive code editor below.

const originalArray = ['apple', 'banana', 'cherry', 'apple', 'banana'];

const uniqueArray = [...new Set(originalArray)];

uniqueArray.forEach((value) => {
document.write(value + "<br>");
});

Have fun coding!


Intersted in seeing some videos? Check out my recent uploads.

What Are MCPs? I Used Cursor to Create a Repo and Push Code to GitHub
25:55

October 8th, 2025

What Are MCPs? I Used Cursor to Create a Repo and Push Code to GitHub

Understanding Docker Multi-Stage Builds (with Example) - Fast and Efficient Dockerfiles
21:26

September 1st, 2025

Understanding Docker Multi-Stage Builds (with Example) - Fast and Efficient Dockerfiles

3 Tips to Optimize Docker Images | Reduce Size, Improve Performance & Security
19:04

August 22nd, 2025

3 Tips to Optimize Docker Images | Reduce Size, Improve Performance & Security

Passwordless Auth in Node.js with Magic Links (Step-by-Step)
26:47

July 7th, 2025

Passwordless Auth in Node.js with Magic Links (Step-by-Step)

One Security Check Most Devs Forget in Their Signup Flow
10:11

May 20th, 2025

One Security Check Most Devs Forget in Their Signup Flow

How to Implement OAuth in Your Node.js Backend (GitHub & Google Login)
30:03

May 15th, 2025

How to Implement OAuth in Your Node.js Backend (GitHub & Google Login)

Next.js JWT Auth Explained: Cookies, Refresh Tokens & 2FA (No Auth.js)
01:02:51

May 6th, 2025

Next.js JWT Auth Explained: Cookies, Refresh Tokens & 2FA (No Auth.js)

Add 2FA to Your Node.js App with otplib
44:19

April 6th, 2025

Add 2FA to Your Node.js App with otplib

How to Run PostgreSQL with Docker Locally
25:48

March 31st, 2025

How to Run PostgreSQL with Docker Locally

Node.js Auth API with JWT, PostgreSQL & Prisma | Vibe Coding
01:23:24

March 30th, 2025

Node.js Auth API with JWT, PostgreSQL & Prisma | Vibe Coding

Create Stunning Emails in React — React-Email & Resend
33:53

March 6th, 2025

Create Stunning Emails in React — React-Email & Resend

Complete Guide — React Internationalization (i18n) with i18next
56:15

February 17th, 2025

Complete Guide — React Internationalization (i18n) with i18next

Build an Animated FAQ Accordion Component in React and Tailwind CSS
30:25

February 8th, 2025

Build an Animated FAQ Accordion Component in React and Tailwind CSS

Learn Shell Scripting With Me Using ChatGPT
47:12

January 30th, 2025

Learn Shell Scripting With Me Using ChatGPT

How to Build a Mega Menu in Next.js – Step-by-Step Tutorial
36:33

January 20th, 2025

How to Build a Mega Menu in Next.js – Step-by-Step Tutorial

The Easiest Way to Make Your Footer Stick to the Bottom || CSS Grid or Flexbox
11:00

January 13th, 2025

The Easiest Way to Make Your Footer Stick to the Bottom || CSS Grid or Flexbox

Form Validation in Next.js with React Hook Form & Zod
27:47

October 31st, 2024

Form Validation in Next.js with React Hook Form & Zod

Styling Active Links in Next.js
08:31

January 20th, 2024

Styling Active Links in Next.js

What is Bun — A JavaScript All-in-one Toolkit
29:01

January 17th, 2024

What is Bun — A JavaScript All-in-one Toolkit

Copy and Paste with JavaScript — 7 lines of code 🔥
01:00

December 27th, 2023

Copy and Paste with JavaScript — 7 lines of code 🔥

Migrate a Node.js Project to use Bun — 60 seconds ⚡️
01:00

December 25th, 2023

Migrate a Node.js Project to use Bun — 60 seconds ⚡️

Build a Slackbot With Slack API and Node.js
38:16

December 19th, 2023

Build a Slackbot With Slack API and Node.js

Technical Writing & Technical Blogging [w/ Asaolu Elijah]
17:58

December 30th, 2020

Technical Writing & Technical Blogging [w/ Asaolu Elijah]