Methods of Array in JavaScript

JavaScript arrays are used to store multiple values in a single variable.

Array Syntax

var array-name = [item1, item2, …];

var colors = [“Red”,”Green”,”Black”];

Methods of Array

  1. Pop

The pop() method removes the last value of an array permanently will return the remaining values.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.pop());

alert(mdArr);

Result:5 and 5,6,7,8,9

  1. Push

The push() method adds the new value in the array at the end and will return the new set of array values.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.push(6,8,9));

alert(mdArr);

Result:9 and 5,6,7,8,9,5,6,8,9

  1. Shift

The shift() method is just like pop method. The only difference is it will remove the first value from an array permanently and return the remaining values.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.shift());

alert(mdArr);

Result:5 and 6,7,8,9,5

  1. Unshift

The unshift() method is the opposite of shift() method. The only difference is it will add the new values at the beginning of an array and return the new set of array values.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.unshift(3,2,1));

alert(mdArr);

Result: 9 and 3,2,1,5,6,7,8,9,5

  1. indexOf()

It will display the index of a given array element. If a number is appearing twice in an array, then it will return the index of first occurrence of a given number.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.indexOf(5));

Result: 0

For Invalid value:

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.lastIndexOf(10));

Result: -1

  1. lastIndexOf()

It will display the index of a given array element. If a number is appearing twice in an array, then it will return the index of last occurrence of a given number.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.lastIndexOf(5));

Result: 5

For Invalid value:

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.lastIndexOf(10));

Result: -1

  1. join()

It will join all array elements into a string specified by a separator.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.join(‘’));//no space

Result: 56785

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.join(‘ ’));//added a space

Result: 5 6 7 8 5

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.join(‘*’));

Result:5*6*7*8*5

  1. slice()

Syntax: slice(Begin Index, End Index)

Begin index will take the position of a given array element and will display

values depending upon the End Index value given. Last index value will not

be included.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.slice(2, 4));

Result: 7,8

It will take values from 2nd index i.e. 7 and will return values till 3rd index. 4th index value will not be included.

For Invalid values:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.splice(8, 3));

Result: Blank Alert

  1. splice()

Syntax: slice(Begin Index, No.of values)

Begin index will take the position of a given array element and will display

values depending upon the value given for No.of values given.

Example:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.splice(2, 4));

Result: 7,8,9,5

It will take values from the 2nd index i.e. 7 and will return upto 4 values.

For Invalid values:

var mdArr = [5,6,7,8,9,5];

alert(mdArr.splice(8, 3));

Result: Blank Alert

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s