javascript / beginner
Snippet
Storing Unique Values with Set
The Set object lets you store unique values of any type. If you try to add the same value twice, it will only be stored once. This is perfect for managing a unique list of categories or tags in your application.
snippet.js
1
2
3
4
5
6
const tags = new Set();tags.add("javascript");tags.add("nextjs");tags.add("javascript");const totalUnique = tags.size;
nextjs
Breakdown
1
new Set()
Creates a new collection that automatically prevents duplicate entries.
2
tags.size
A property that returns the number of unique elements currently in the Set.