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.

Array in ES6

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 in ES6

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:

Array in ES6

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:

Array in ES6

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 in ES6

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 in ES6

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 in ES6

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. 


Related Topics

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

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...

5 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.