• +91-748-689-9976
  • info@inspireninfotech.com
  • Surat, Gujarat, India
JAVA SCRIPT
Arrays in JavaScript

Arrays in JavaScript

Arrays

The array object allows you to store multiple values in one variable. It stores data in sequence form having same type. JavaScript arrays can store any valid value, including strings, numbers, objects, functions, and even other arrays, thus making it possible to create more complex data structures such as an array of objects or an array of arrays.

Creating an array

You can create an array in JavaScript by creating a list of elements separated by comma and enclosed in square brackets [].

1var firstArray=[element0, element1, element2……element];

Let us take some examples of arrays:

1
2
var colorName=[“red”, “green”, “purple”];
var fruitsName=[“banana”, “apple”, “orange”];

An array is an ordered collection of values. Each value in an array is called an element, and each element has a numeric position in an array, known as its index.

In above example, the elements can be accessed as follows:

firstArray[0]=”red”;

firstArray[1]=”green”;

firstArray[2]=”purple”;

Accessing array elements in JavaScript

The elements in an array can be accessed by using their index with square bracket notation. An index is a number that represents the position of an element in an array. The index value starts with 0 and increases by an increment by one. The first position will be [0], second will be at [1], the third position will be [2] and so on. So, array of six elements would have indexes from 0 to 5.

It will result as AppleBananaMango

How to get the length of Array in JavaScript?

When you have created an array in JavaScript, you may need to know the length of that array i.e. you may require the number of elements contained in the array. You can find the number of elements of array by using the length property of array. Array length is always greater than the index of any of its elements. Let us take an example of an array to find its length.

It will give the length of array vegetable as 3.

Looping of array elements

When you need to access each element of an array, you can do that with the help of for loop. There are four types of for loop in JavaScript which can be used in your code.

  • for loop using length property of an array
  • for-of loop
  • for-in loop
  • forEach()

Let us take an example of for loops

It result as shown below:

array in javascript, www.programmingtutorial.in

How to add new element to an array?

You can add a new element to an existing array at the end of an array by using push() method.

Adding an element to an array

Orange,Mango,Papaya,Grapes
Orange,Mango,Papaya,Grapes,Banana

How to remove last element of an array?

You can add a new element to an array, but what about removing an element? The last element of an array can be easily removed by using pop() method.

It results as

Removing an element from an array

Orange,Mango,Papaya,Grapes
The removed element is given below
Grapes
The remaining array list after removal of last element
Orange,Mango,Papaya

How to add or remove an element at any position of an array?

Just like an element is added or removed at last position by using push() and pop() method, an element can also be added or removed at any position of an array. This can be achieved by using splice() method of an array. Its syntax is:

arrayName.splice(startingIndex, deleteCount, Elem1, Elem2,…..ElemN);

The method takes three parameters, the index from where the splicing of an array will occur, second is the deleteCount which specifies how many elements will be removed and the third parameter used to add elements. If you don’t want to delete an element then keep deleteCount as 0. The splice() method modifies an array. Let us take some examples:

The result of above code is

Removing and inserting element from/in an array

Orange
Mango,Banana,Kiwi,Papaya,Grapes
Mango,Banana,Pears,Grapes

How to extract a portion of an array?

An array can be extracted in JavaScript by keeping original array intact with the help of slice() method. In other words, you can say the slice() method creates a subarray. Its syntax is:

array.slice(startingIndex, endingIndex)

This method takes two parameters as startingIndex at which extraction will be started and an optional endingIndex before which the extraction will be stopped. If endingIndex has not been given, it will extract all the element falling after the startingIndex. Let us take an example to better understand the concept.

Extracting elements from an array

Mango,Papaya
Mango,Papaya,Grapes

How to merge two arrays in JavaScript?

If you have two arrays in JavaScript and you want to combine them as one, you can do that with the help of concat() method of JavaScript. This method does not alter an existing array instead it creates a new array by combining two or more individual arrays. Let us write a code for concatenating arrays.

Arrays concatenation

Orange,Apple,Kinnu,Watermelon,Melon,Grapes,Pears
Orange,Apple,Kinnu,Watermelon,Melon,Grapes,Pears,Almonds,Walnuts,Cashews

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow us on Social Media