ES6 | Array

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 declaringvar JS = ["ES1", "ES2", "ES3", "ES4", "ES5", "ES6"]; // Initializing after declaringJS[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 declaringvar JS= ["ES1", "ES2", "ES3", "ES4", "ES5", "ES6"]; // Accessing array elementconsole.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 arrayvar 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.  
Note: In JavaScript, arrays use numbered indexes, objects use named indexes. 
sooda367
akshaysingh98088
ES6
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
JavaScript | Promises
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?