JavaScript Array Methods

Newaj Ahmed
Nov 1 · 2 min read
  1. Converting Arrays to Strings: To convert an Array to string we use the method toString( ).

Example:

var colors= [“Red”, “Yellow”, “Orange”, “Blue”]; console.log( colors.toString( ));

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

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.pop();

After

[“Banana”, “Orange”, “Apple”]

3. Pushing: Push( ) method adds a new element at the end of and array.

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.push(“Grape”);

After

[“Banana”, “Orange”, “Apple”, “Mango”, ”Grape”];

4. Shifting: The shift( ) method removes the element in the first index and the elements gets lower index.

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.shift( );

After

[ “Orange”, “Apple”, “Mango”, ”Grape”];

5. unshifting: The unshift( ) method adds a new element in the beginning of the array.

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.unshift( Kiwi );

After

[ “Kiwi“,Orange”, “Apple”, “Mango”, ”Grape”];

6. Changing Elements: The elements can be changed by accessing their index numbers.

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits[0] = “Strawberry”;

After

[“Strawberry”, “Orange”, “Apple”, “Mango”];

7. Deleting Elements: Elements can be deleted using Javascript operator delete.

Example:

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
delete fruits[0];

//In this case the the first element changes to undefined.

8. Splice: The splice( ) method can be used to add new items to an array.

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.splice(2, 0, “Strawberry”, “Kiwi”);

After

[“Banana”, “Orange”, “Strawberry”, “Kiwi”, “Apple”, “Mango”];

9. Remove with splice :to remove elements without leaving "holes" in the array we can also use splice( ).

Example:

Before

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.splice( 0 , 1);

After

[ “Orange”, “Apple”, “Mango”, ];

10. Merging (Concatenating) Arrays: The concat ( )method is used to merge a new array with existing arrays.

Example:

Before

var myGirls = [“Lisa”, “Anna”];
var myBoys = [“Jack”, “Ramzi”, “Drake”];
var myChildren = myGirls.concat(myBoys);

After

Lisa, Anna, Jack, Ramzi, Drake

Reference: W3school

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade