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 Methods

Converting Arrays to Strings

JavaScript method toString() is used to convert an array to a string of (comma separated) array.

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours.toString()); // Output // Red,Yellow,Orange,Blue

Convert Arrays to String Using Separator

The join() method is used to join all array elements into a string by using separator

It works just like toString(), but in addition you can specify the separator.

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours.join(" - ")); // Output // Red - Yellow - Orange - Blue

Popping and Pushing

Popping

Popping means remove the element from an array.

The pop() method removes the last element from an array:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.pop(); console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Red", "Yellow", "Orange"]

To Remove the First Element from the Array, you can use shift(); method in Place of pop();

Pushing

Pushing means Adding Element to an Array.

The push() method adds a new element to an array (at the end):

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.push("Brown"); console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Red", "Yellow", "Orange", "Blue","Brown"]

To Remove the Last Element from the Array, you can use unshift(); method in Place of push();

Deleting Elements

To Delete the Element from an Array delete method is used with index of the Element.

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); delete colours[2]; console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Red", "Yellow", , "Blue"]

By Using delete method, you can delete any element from an array, you just need to know its Index.

Using delete may leave undefined holes in the array. Use pop() or shift() instead.

Here, When we used the delete it deleted that element and left that space that is why, , is showing at that place. that means there is Element Exist but with Blank Value.

Adding Element at Any Position (Splicing an Array)

The splice() method is used to add new items to an array (at any place):

Syntax

variableName.splice(initialPosition, noOfElements, parameterToAdded)

The first parameter initialPosition defines the position where new elements should be added.

The second parameter noOfElements defines how many elements should be removed.

The parameterToAdded define the new elements to be added.

Example

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.splice(2, 0, "Brown", "Black"); console.log(colours); // output // ["Red", "Yellow", "Orange", "Blue"] // ["Red", "Yellow", "Brown","Black","Orange", "Blue"]

The first parameter (2) defines Adding Elements should start after 2nd Position

The second parameter (0) defines how many elements should be removed.

The ("Brown", "Black") define the new elements to be added after 2nd Position.

Removing Element of Any Position (Splicing an Array)

You can also use splice() to remove elements without leaving "holes" in the array:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.splice(0,1); console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Red", "Orange", "Blue"]

The first parameter (0) defines the position where new elements should be added

The second parameter (1) defines how many elements should be removed.

This is Recommended Method, to Remove Element at any Index.

Merging (Concatenating) Arrays

Concatenation Refers to Merging Arrays.

The concat() method creates a new array by merging existing arrays:

Syntax:

arrayOne.concat(arrayTwo,arrayThree, ...);

Example:

var frd1 = ["Ram", "Rohan"]; var frd2 = ["Ronak", "Rohit", "Rahul"]; var frd3 = ["Ramesh", "Shivam"]; var allFriends = frd1.concat(frd2,frd3); console.log(allFriends); // Output // ["Ram", "Rohan", "Ronak", "Rohit", "Rahul", "Ramesh", "Shivam"]

Here,

frd2 , frd3 Array is added to the frd1

Slicing(Cutting) an Array

The slice() method cuts out a piece of an array into a new array.

Syntax:

var colours = ["Red", "Yellow", "Orange","Blue"]; var newColours = colours.slice(1,3); console.log(newColours); console.log(colours); //Output // ["Yellow", "Orange"] // ["Red", "Yellow", "Orange", "Blue"]

Here,

slice(a,b) method cuts out the element between the index a and index b

Sorting Array

Sorting Alphabetically

The sort() method is used to sort an array alphabetically

Syntax

arrayName.sort();

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.sort(); console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Blue", "Orange", "Red", "Yellow"]

Reverse Sorted

The reverse() method is used to sort an array in Reverse Order of the Entered Array.

Syntax

arrayName.reverse();

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; console.log(colours); colours.reverse(); console.log(colours); // Output // ["Red", "Yellow", "Orange", "Blue"] // ["Blue", "Orange", "Yellow", "Red"]

Find The Index of Element By Value

The indexOf() method searches an array for an element value and returns its position.

Example:

var colours = ["Red", "Yellow", "Orange","Blue"]; var myIndex = colours.indexOf("Orange"); console.log(myIndex); // Output // 2

If any Element is present more than once in Array List, it returns the position of the first occurrence.

The Method Array.indexOf() returns -1 if the Element is not found.

Find The Index of Last Occurrence of Element By Value

Array.lastIndexOf()  returns the position of the last occurrence of the specified element.

Example:

var colours = ["Red", "Yellow", "Orange","Blue","Yellow"]; var myLastIndex = colours.lastIndexOf("Yellow"); console.log(myLastIndex); // Output // 4