Open In App

ES6 Array

Improve
Improve
Like Article
Like
Save
Share
Report

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 to 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 updated but can not be deleted.

Initializing an array: Initializing or declaring an array is piece of cake. The below program implements the initialization of an array. 

Example: 

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 access the array elements. The indexing of the array always starts with 0. 

Example: 

javascript




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


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.

Example 1: 

javascript




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]);


Output: 

ES6
2015
ES8
2017
ES10
2019

Example 2: 

javascript




var Geeks = new Array(6)
for (var i = 0; i < Geeks.length; i++) {
    Geeks[i] = (i * 2) / 3
    console.log(Geeks[i])
}


Output: 

0
0.6666666666666666
1.3333333333333333
2
2.6666666666666665
3.3333333333333335

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

Methods Description
concat()

This method is used to merge two or more arrays together.

var newArray = oldArray.concat(value1 , [ value2, [ ...,[ valueN]]])
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.

every((element, index, array) => { /* … */ })
filter()

This method creates a new array with elements that follow or pass the given criteria and conditions.

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
find()

Filter elements through the function, and return first/all values that make it return true.

array.find(function(currentValue, index, arr),thisValue);
forEach()

This method is used to iterate through its elements and manipulate them.

array.forEach( callback, thisObject )
Array.from()

This change all thing that is 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.

Array.from(object, mapFunction, thisValue)
indexof()

This method is used to find the index of the first occurrence of the search element provided as the argument to the method.

array.indexOf(element, start)
join()

This method is used to join the elements of an array into a string.

array.join(separator)
lastIndexOf()

Look for an item starting from position pos, and return the index or -1 if not found.

array.lastIndexOf(element, start)
map()

This method creates a new array by calling the provided function in every element.

map((element, index, array) => { /* … */ })
Array.of() This creates an array from every argument passed into it.
pop()

Removes the last element from an array and returns that element.

arr.pop()
push()

Adds one or more elements to the end of an array and returns the new length of the array.

arr.push(element0, element1, … , elementN)
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

array.reduce( function(total, currentValue, currentIndex, arr),initialValue )
reverse()

Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.

arr.reverse()
shift()

This method removes the first element of the array thus reducing the size of the original array by 1.

arr.shift()
slice()

Creates a new array, and copies elements from position start till the end (not inclusive) into it.

arr.slice(begin, end)
some()

This method checks if at least one of the array’s items passed the condition. If passed, it returns ‘true’ otherwise ‘false’.

arr.some(callback(element,index,array),thisArg)
sort()

This method is used to sort the array in place in a given order according to the compare() function.

arr.sort(compareFunction)
splice()

This method is used to modify the contents of an array by removing the existing elements.

Array.splice( index, remove_count, item_list )
unshift()

This function increases the length of the existing array by the number of elements added to the array.

array.unshift(element1, element2, ..., elementX)

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



Last Updated : 14 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads