Skip to content
Related Articles

Related Articles

Improve Article

ES6 | Array

  • Last Updated : 31 Aug, 2021

An array is a homogeneous collection of values. It is a single variable that is used to store different elements. It is often used when we want to store a list of elements and access them by a single variable. Unlike most languages where the array is a reference to the multiple variables, an ES6 array is a single variable that stores multiple elements. We can use normal variables (v1, v2, v3, ..) when we have a small number of objects, but if we want to store a large number of instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.
Characteristics of an Array: 
 

  • Indexing is available in an array so random access of elements using the array index is possible.
  • Multiple elements can be stored using a single variable name.
  • Traversal through an array becomes easy using a loop.
  • Sorting becomes easy as it can be accomplished by writing fewer lines of code in an array.
  • Array values can be modified or update but can not be deleted.

Initializing an array: Initialize or declare an array is peace of cake. The below program implements the initialization of an array. 
 

  • Program: 
     

javascript




// Initializing while declaring
var JS = ["ES1", "ES2", "ES3", "ES4", "ES5", "ES6"];
 
// Initializing after declaring
JS[0] = "ES6";
JS[1] = "ES6";
JS[2] = "ES6";
JS[3] = "ES6";

Accessing array elements: To access any specific array element we will need to know the index number of that element. The array index number is used to accessing the array elements. The indexing of array always starts with 0. 
 

  • Program: 
     

javascript




<script>
// Initializing while declaring
var JS= ["ES1", "ES2", "ES3", "ES4", "ES5", "ES6"];
 
// Accessing array element
console.log(JS[4])
console.log(JS[5])
</script>
  • Output: 
     
ES5
ES6

Array Object: The Array constructor can be passed as a numeric value that represents the size of the array and array elements are comma-separated values.
 

  • Program 1: 
     

javascript




<script>
var js = ["ES6", 2015, "ES8", 2017, "ES10", 2019];
 
// len contains the length of the array
var len = js.length;
for (var i = 0; i < len; i++)
    console.log(js[i]);
                 
</script>                   
  • Output: 
     
ES6
2015
ES8
2017
ES10
2019
  • Program 2: 
     

javascript




<script>
var Geeks = new Array(6) 
for(var i = 0; i < Geeks.length; i++)
{
   Geeks[i] = (i * 2)/3
   console.log(Geeks[i])
}
</script>                                   
  • Output: 
     
0
0.6666666666666666
1.3333333333333333
2
2.6666666666666665
3.3333333333333335

Array Methods: There are lots of array methods introduced in ES6. 
 

MethodsDescription
concat()This method is used to merge two or more arrays together.
every()This function checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.
filter()This method creates a new array with elements that follow or pass the given criteria and condition.
find()Filter elements through the function, return first/all values that make it return true.
forEach()This method used to iterate through its elements and manipulate them.
Array.from()This change all thing that are array-like or iterable into true array especially when working with DOM, so that you can use other array methods like reduce, map, filter and so on.
indexof()This method is used to find the index of the first occurrence of the search element provided as the argument to the method.
join()This method is used to join the elements of an array into a string.
lastIndexOf()Look for item starting from position pos, return the index or -1 if not found.
map()This method create new array by calling the provided function in every element.
Array.of()This create array from every arguments passed into it.
pop()Removes the last element from an array and returns that element.
push()Adds one or more elements to the end of an array and returns the new length of the array.
reduce()The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value – MDN
reverse()Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.
shift()This method removes the first element of the array thus reducing the size of the original array by 1.
slice()Creates a new array, copies elements from position start till end (not inclusive) into it.
some()This method check if at least one of array’s item passed the condition. If passed, it return ‘true’ otherwise ‘false’.
sort()This method used to sort the array in place in a given order according to the compare() function.
splice()This methods is used to modify the contents of an array by removing the existing elements.
unshift()This function increases the length of the existing array by the number of elements added to the array.

Note: In JavaScript, arrays use numbered indexes, objects use named indexes.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :