JavaScript Array

Introduction

A JavaScript Array is used to store multiple values in a single variable.

Example: Array of single digit numbers

myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Creating an Array

An array can be created using following two ways:

  1. Using array literal

var myArray = [ ];

  1. Using JavaScript Keyword new

var myArray = new Array( );

Access an element of an Array

An element of an array can be accessed by referring its position also known as index number.

Suppose, we have an array myArray of different computer languages.

myArray = [“C”, “C++”, “JavaScript”, “ABAP”, “Java”];

The index of an array starts from 0. Therefore, [0] will be the first element and [1] will be the second element.

We can access any of the element by referring its index as shown below:

myElement = myArray[2];

Output: “JavaScript”

Looping Array Elements

To Loop each of the element, we can use a for loop as shown below:

Example: In given example we will check if the array of devices has laptop or not.

var device = [“mobile”, “computer”, “laptop”, “earphone”]

var dLength = device.length;

for (i = 0; i < dLength; i++)

{

If (device[i] = “laptop”)

console.log(“Yes, array has laptop as a device!”);

}

 

Operations on JavaScript Array

We can perform following operations on an Array:
In given examples, we will perform operations on an array numbers = [1,2,3,4];

OperationDescriptionExample
pop()This method removes the last element of an array.numbers.pop();

Output: [1,2,3]

push()This method adds a new element in the end.numbers.push(5);

Output: [1,2,3,4,5]

lengthThis is a property of array and it returns the length of an array.numbers.length;

Output: 4

shift()This method removes the first element of an array and shifts all the other elements to the lower index.numbers.shift();

Output: [2,3,4]

unshift()This method adds a new element at the beginning.numbers.unshift(0);

Output: [0,1,2,3,4]

forEach()This method calls a function once for each array element.numbers.forEach(function(num){

if(num == 2)

console.log(“2 is there”);

});

Output: 2 is there

toString()This method converts an array to a string of array values separated by comma.number.toString();

Output: 1,2,3,4

deleteThis method converts an element to undefined. It is recommended to pop() or shift() to remove an element in place of delete.delete numbers[0];

console.log(numbers[0]);

Output: undefined

splice(a,b)This method is used to delete and insert new items.

Here a is the index where we want to insert/delete items and b is the number of elements that will be deleted.

Anything added after these parameters i.e. a and b will be inserted at the index a.

1. Deleting items

numbers.splice(0,1);

console.log(numbers);

Output: [2,3,4]

2. Adding items

numbers.splice(0,1,-1);

console.log(numbers);

Output: [-1,2,3,4]

slice(a,b)This method slices out a part of an array into a new array. Here, a is the index from where the slice begins and b is the index where it ends, excluding b. If there is no ending index i.e. b, then all the rest elements beginning from a is sliced.var newSet = numbers.slice(1,3)

console.log(newSet);

Output: [2,3]

concat()This method concatenates two arrays into a new array.var numbers2 = [5,6,7,8]

var mergeSet = numbers.concat(numbers2);

console.log(mergeSet);

Output: [1, 2, 3, 4, 5,6,7,8]

sort()This method sorts an array alphabetically/numerically in ascending order.var set = [1,2,3,4,0];

set.sort();

console.log(set);

Output: [0,1,2,3,4]

reverse()This method reverses the elements of an array.

This method can be used to sort an array in descending order which is already sorted in ascending order.

numbers.reverse();

console.log(numbers);

Output: [4,3,2,1]

indexOf()This method searches for an element value and returns its position into a new variable.var a = numbers.indexOf(1);

console.log(a);

Output: 0

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.