Set vs Array in JavaScript
Last Updated :
28 Dec, 2023
In JavaScript, Set and Array are common data structures used to store collections of data. These data structures are different because of their unique features. Let us look at the implementation of both of these data structures.
Sets are used to store collections of unique value without allowing duplication. The set is similar to the array and supports both insertion and deletion methods of the array. Sets are faster than arrays in terms of searching as they use a hash table internally for storing data and can be used to replace duplicates from other data types.
Syntax:
new Set([it]);
Parameter:
- it: It is an iterable object whose all elements are added to the new set created, If the parameter is not specified or null is passed then a new set created is empty.
Return Value:
A new set object.
Example: In this example, we will implement a set.
JavaScript
let sample = new Set();
sample.add("Hello");
sample.add(1)
sample.add("Bye")
sample.add("@");
for (let item of sample) {
console.log(item);
}
The array is a data structure that is used to store data in a sequential manner of the same type. Arrays allow duplication of data and data is indexed which means that the data can be accessed according to its unique index.
Syntax:
let arrayName = [value1, value2, ...]; // Method 1
let arrayName = new Array(); // Method 2
Example: In this example, we will implement an array and access its element.
JavaScript
let sample = new Array();
sample.push("Hello");
sample.push("1")
sample.push("Bye")
sample.push("@");
console.log(sample);
console.log(sample[3])
Output[ 'Hello', '1', 'Bye', '@' ]
@
What to use?
The usage of the data structure depends on the requirement. If you want to have a unique list of items where each element can only be present once and also want faster access to the elements then use set otherwise if you want to access the element according to its insertion order then use array.
Difference between Set and Array
|
Collection of unique value | Sequential collection of data |
Duplication is not allowed | Duplication is allowed |
Accessing elements is faster | Searching is slow comparatively |
Elements are accessed using hash table | Elements are accessed using index |
Similar Reads
Set to Array in JS or JavaScript This article will show you how to convert a Set to an Array in JavaScript. A set can be converted to an array in JavaScript in the following ways:1. Using Spread OperatorThe JavaScript Spread Operator can be used to destructure the elements of the array and then assign them to a new variable in the
2 min read
JavaScript Array Methods To help you perform common tasks efficiently, JavaScript provides a wide variety of array methods. These methods allow you to add, remove, find, and transform array elements with ease.Learn More on JavaScript ArrayTable of Content1. JavaScript Array length 2. JavaScript Array toString() Method3. Jav
8 min read
Types of Arrays in JavaScript A JavaScript array is a collection of multiple values at different memory blocks but with the same name. The values stored in an array can be accessed by specifying the indexes inside the square brackets starting from 0 and going to the array length - 1([0]...[n-1]). A JavaScript array can be classi
3 min read
Set in JavaScript A Set in JavaScript is used to store a unique collection of items, meaning no duplicates are allowed. Sets internally use a hash table which makes search, insert and delete operations faster than arrays. Please note that a hash table data structure allows these operations to be performed on average
4 min read
JavaScript Array push() Method The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array, increasing its length by the number of elements added.Syntaxarr.push(element0, element1, ⦠, elementN);ParametersThis method contains as many numb
3 min read