Array in ES6
Array in ES6
The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store more than one or a number of values into a single variable. In an array, the value should be stored chronologically.
In other words,
An array can be defined as a group of similar types of values.
Or
An array can be determined as the set of values that have the same data type.
The array can be used to store various types of values such as string, objects, another array, number, function, etc. These values help us to build a complex kind of data structure.
Features of an array
Some features of an array are mentioned below-
- The arrays are static. It means once we initialized a value to the array, it should not be changed.
- When we declare an array, it allocates the memory blocks in a sequential manner.
- Each memory block is used to present the array element.
- The Array should be declared before it is used as a variable.
- The array initialization is a process to pass a value to the array element.
- We can only modify or update the value of an array element, cannot delete it.
- We can identify the array element by the unique name (integer type) known as Index or subscript.
Syntax
var array_name = [value1, value2,…..valueN] ; // with the help of Array literal
We can also declare an array by using another way.
Syntax
var array_name = new Array (); // with the help of keyword new
Initialization or Declaration of an array
“The Initialization is a process that refers to store or assign the value to the array element at the time of construction.”
var array_name = [value1, value2,…..valueN];
The brackets [] are also called a dimension of the array.
“Declaration is a process that refers to introduce a new name/ element to the program.”
var array_name = new Array ();
Important Point: The maximum length allowed for an array is 4,294,967,295.
Accessing Array Element
The address index of an array starts from 0. The array name associated with subscript is considered as an element of an array.
Syntax
array_name[sunscript];
Example
var value; value = [1, 2, 3, 4, 5]; console.log (value[1]); console.log (value[2]);
Output
After the execution of the code, we got the following output:
Array Object
It is also called as an Array Constructor. An array can also be created with the help of an array constructor. We can pass an array object as given below-
- A numeric value is used to indicate the size of an array.
- A list of value separated with the help of comma (,).
Example- A single numeric value
var value = new Array(4); // Value represents the size of an array var p; for (p=0; p<value.length; p++) { value[p] = p*4; console.log (value[p]); }
Output
After successful execution of the code, we got the following output:
Example – The Comma separated value
var value = new Array(1, 2, 3, 4, 5, 6); // The Values separated with comma var q; for (q=0; q<value.length; q++) { console.log (value[q]); }
Output
After the execution of the code, we got the following output:
ECMAScript Arrays
ECMAScript and JavaScript support the following types of arrays:
- Multi-dimensional Array
- Array passing to the function
- Array returns from the function
Multi-dimensional Array
ECMAScript 6 supports the concept of a multidimensional array. The term Multi-dimensional can be defined as an array associated with other arrays for the value. We cannot directly use the multidimensional array in JavaScript, but we can use it via the single-dimensional array. The two-dimensional is also a part of multi-dimensional array.
Declaration
Here, we have the syntax of the multi-dimensional array.
var array-name =[[value1, value2, value3],[num1, num2, num3]];
Accessing multi-dimensional array
var array_name[initial_index][referenced_index];
Example
var value = [[1, 2, 3],[4, 5, 6]]; console.log (value[0][0]); console.log (value[0][1]); console.log (value[0][2]); console.log (value[1][0]); console.log (value[1][1]); console.log (value[1][2]);
Output
After the execution of the code, we got the following output:
Array passing to the function
If we want to pass an array (as an argument) to the function, we need to specify the name of an array (a reference name) without brackets.
Here, we have an example to understand the concept better.
Example
var color = new Array[‘Yellow’, ‘Red’, ‘Blue’, ‘Green’, ‘Purple’, ‘Pink’]; function display (color) { for(var a = 0; a<color; a++) { console.log (color[a]) } } display (color)
Output
After the execution of the code, we get the following output.
Array returns from the function
This kind of array allows a function to return an array.
We will try to understand it with the help of the following example.
Example
function display () { return new Array (‘Black’, ‘Blue’, ‘Sky blue’, ‘Magenta’, ‘Red’) } var color = display () for(var p in color) { console.log (color[p]) }
Output
After the execution of the code, we get the following output.
Array Methods
The Array methods enable us to work with stored data in an array. We can quickly execute the complex tasks of an array with the help of an array method.
Array Destructuring
The term Destructuring is referred to break the difficult data structures into the smooth and simpler parts. The destructuring syntax is used to extract the small portion from the array and the objects. We can assign and declare a variable by using a destructuring array.
Object Destructuring
Destructuring is used to break the complicated data structures into simpler parts. In JavaScript, the complicated structure is an array or an object. The Destructuring objects are used to impart the object and array in small segments.
Destructuring Assignments
The Destructuring assignment is a new feature provided by the ECMAScript 6. The feature allows us to impart the particular item from an array and the object and put them into a variable. It helps us to change the manner of coding and reduce the code. The destructuring assignment is a mechanism to break down the data structure. The Destructuring assignment is considered as the JavaScript expression that allows us to extract the data from a set, map, array, and the properties of the object in variables.