const myArr = [1, 2, 3]; console.log(myArr[0]); // Output: 1 const mySet = new Set([1, 2, 3]); console.log(mySet[0]); // undefined const arr1 = [1,2,3,3]; // Initializes an array with 4 items const set1 = new Set( [1, 2, 3, 3] ); // Initializes with 3 items // method 1 var arr1 = [ ]; // method 2 var arr2 = new array(); // Initializes an empty set const set1 = new Set(); // Initialize with value const set2 = new Set([1, 2, 3]); Initialize a set Duplicate values are not allowed Items can be accessed using index Items cannot be accessed using index Initialize an array Duplicate values are allowed VSARRAY SET [🍎, 🍎, 🍐, 🍐, 🍊] [🍎,🍎, 🍐, 🍐, 🍊] [🍎, 🍎, 🍐, 🍐, 🍊] [🍎, 🍐, 🍊]
By combining features of array and a set in javascript, we get more powerful code. Example Easy shortcut to remove duplicates from an array Brought to you by Ideas2IT TRICKS TO REMOVE DUPLICATES FROM ARRAY // array with duplicates let array1 = [1,1,2,2,3,3,4,4]; // create a Set with array let array2 = Array.from(new Set(array1)); console.log(array2); // [ 1, 2, 3, 4 ]

Array vs set in JavaScript

  • 1.
    const myArr =[1, 2, 3]; console.log(myArr[0]); // Output: 1 const mySet = new Set([1, 2, 3]); console.log(mySet[0]); // undefined const arr1 = [1,2,3,3]; // Initializes an array with 4 items const set1 = new Set( [1, 2, 3, 3] ); // Initializes with 3 items // method 1 var arr1 = [ ]; // method 2 var arr2 = new array(); // Initializes an empty set const set1 = new Set(); // Initialize with value const set2 = new Set([1, 2, 3]); Initialize a set Duplicate values are not allowed Items can be accessed using index Items cannot be accessed using index Initialize an array Duplicate values are allowed VSARRAY SET [🍎, 🍎, 🍐, 🍐, 🍊] [🍎,🍎, 🍐, 🍐, 🍊] [🍎, 🍎, 🍐, 🍐, 🍊] [🍎, 🍐, 🍊]
  • 2.
    By combining featuresof array and a set in javascript, we get more powerful code. Example Easy shortcut to remove duplicates from an array Brought to you by Ideas2IT TRICKS TO REMOVE DUPLICATES FROM ARRAY // array with duplicates let array1 = [1,1,2,2,3,3,4,4]; // create a Set with array let array2 = Array.from(new Set(array1)); console.log(array2); // [ 1, 2, 3, 4 ]