Set vs Array in JavaScript Last Updated : 28 Dec, 2023 Comments Improve Suggest changes Like Article Like Report 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. JavaScript SetSets 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); } OutputHello 1 Bye @ JavaScript ArrayThe 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 1let arrayName = new Array(); // Method 2Example: 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 ArraySetArrayCollection of unique valueSequential collection of dataDuplication is not allowedDuplication is allowedAccessing elements is fasterSearching is slow comparativelyElements are accessed using hash tableElements are accessed using index Comment More infoAdvertise with us Next Article Set vs Array in JavaScript S shobhit_sharma Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Set +1 More 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 JavaScript Array fill() Method The fill() method in JavaScript is used to fill all the elements of an array from a start index to an end index with a static value. It mutates the original array and returns the modified array. Syntax: arr.fill(value, start, end)Parameters: This method accepts three parameters as described below: P 3 min read JavaScript Array fill() Method The JavaScript Array fill() Method fills a given range of array elements with the given value. This method is used to manipulate the existing array according to our needs.Syntax:arr.fill(value,start,end)Parameters:Value: Value to be filled.Start: Start index (included) and its default value is 0.End 3 min read Javascript Array at() Method The JavaScript Array at() method takes an integer value (index) as a parameter and returns the element of that index. It allows positive and negative integers. For the negative integer, it counts back from the last element in the array.Syntax:at(index);Parameter: This method accepts one parameter th 3 min read JavaScript Array Iteration Methods JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1: 3 min read JavaScript Arrays In JavaScript, an array is an ordered list of values. Each value is called an element, and each element has a numeric position in the array, known as its index. Arrays in JavaScript are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.Array in JavaScriptWhy Use 7 min read Like