Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Array Declaration

You can use Array to store multiple values in a single variable.

Example : Suppose, You have a list of Fruits then for each Fruit you have to create a new variable so with the help of Array you can store the names of All Fruits in a Single Array.

Advantages of using Array

  • Store Multiple Values in a Single Variable.
  • Every Item has a Index by which you can access to that value.
  • You can Store as many Values as you want.

Index

It is the Unique Position Number of the Values you have assigned in Array.

In Every Array Index Starts from 0.

For 1st Value index is 0 , For 2nd Value index is 1, For 3rd Value index is 2 and So on.

Syntax

var arrayName = [ item1, item2, .... ];

Example:

```jsx var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; console.log(fruitName[0]); console.log(fruitName[1]); console.log(fruitName[2]); console.log(fruitName[3]); console.log(fruitName[4]); //output //Apple //Banana //Orange //Kiwi //Grapes ```

Here,

We have Declared fruitName array and have stored names of 5 Fruits.

Then, we have printed fruitName with index 0 in the Console Tab.

Let me explain the index :

In array, Index is the ID of the Value stored in a array, indexing starts from 0, 1, 2, 3 .......

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/425047f6-4b7d-4ecd-beb6-f0e165c6c992/Untitled.png

Total No. of Elements are 5

Change the Value of Array

To Change the Value, We want to know the index of element and store the new value to that index

Syntax

arrayName[index] = "New Value";

Example:

```jsx var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; console.log("Values Before Changing :"); console.log(fruitName[0]); console.log(fruitName[1]); console.log(fruitName[2]); console.log(fruitName[3]); console.log(fruitName[4]); fruitName[1] = "Strawberry"; console.log("Values After Changing :"); console.log(fruitName[0]); console.log(fruitName[1]); console.log(fruitName[2]); console.log(fruitName[3]); console.log(fruitName[4]); //output //Values Before Changing : //Apple //Banana //Orange //Kiwi //Grapes //Values After Changing : //Apple //Strawberry //Orange //Kiwi //Grapes ```

Here,

We are changing the value of fruitName array with index  1 i.e. Banana and Changing it to Strawberry.

Array Length

You can find out how many elements an array has, using the length property:

Syntax

arrayName.length;

Example 1:

var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; var length = fruitName.length; console.log(length); //output 5

Example 2:

In this example, we are trying to get the Last Value of Array without knowing the index.

var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; var length = fruitName.length; var lastFruit = fruitName[length - 1]; console.log(lastFruit); // Output // Grapes

Here,

We have declared fruitName array with 5 Fruits Stored in it.

length variable stores the length of the fruitName array i.e. 5

Then, We are storing value of fruitName[length - 1] to lastFruit variable.

i.e. fruitName[5-1] ⇒ fruitName[4]

Why we subtracted 1 to the array length? because, indexing of variable starts from 0 and fruitName.length gives 5

Lets Understand this in Tabular Form

Array Index

Array Iteration

Printing All Elements of 1-D Array.

We, will be using for Loop

var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; var totalLength = fruitName.length; var i; for(i=0;i

For...Of Loop

There is one more way to Loop through the elements of the Array.

By using For...of Loop.

Syntax

for(variableName of array){ //statements }

Loop will iterate from starting to the End Elements of the Array.

That Means, For Each Element of the Array statements will be executed.

Example

var fruitName=["Apple","Banana","Orange","Kiwi","Grapes"]; for(i of fruitName){ console.log(i); } // Output // Apple // Banana // Orange // Kiwi // Grapes

Did you see the difference?

Now we don't have to find the length of the array. for...of Loop will Execute for Each Element of the Array.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?