DEV Community 👩‍💻👨‍💻

Naveen.S
Naveen.S

Posted on

Filter Unique Values From Array Using JavaScript

The Set object type was introduced in ES6 (or ES2015), and along with the spread operator ..., you can use it to create a new array with only the unique values.

const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)]];

console.log(uniqueArray);
// Output is [1, 2, 3, 5] 

Before ES6, isolating unique values would involve a lot more code than that.

This trick works for an array containing primitive types: undefined, null, boolean, string, and number. If you had an array containing objects, functions or additional arrays, or you'd need a different approach.

Top comments (0)

Classic DEV Post from 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!