Array and Its Methods in Javascript - PART 2

Array and Its Methods in Javascript - PART 2

In PART 1 of this Article, we discussed What is an array in Javascript and a few methods of Array - length, push(), slice(), splice(), concat(), fill(), includes(), isArray()

In this article, we will discuss a few more methods of Array.

Array Methods

Sort()

The sort() method is used to sort the array in ascending order.

below is the syntax to sort an array.

let arr_name = ['xyz','abc','pqr','mno'];
console.log(arr_name.sort());

//The output is as follows - 
//[ 'abc', 'mno', 'pqr', 'xyz' ]

toString(), join()

toString() method returns the string combining all values. the values are comma separated. to specify any custom separator, we have to use join().

Below is the syntax -

let arr_name = ['val1','val2','val3','val4'];
console.log(arr_name.toString());
//output - 
//val1,val2,val3,val4
console.log(arr_name.join('*'));
//output -
//val1*val2*val3*val4

Pop()

The pop() method removes the last element in an array and returns the value that is popped out.

Below is the syntax -

let arr_name = ['val1','val2','val3','val4','val4'];
console.log('The popped value is ',arr_name.pop());
//output - The popped value is  val4
console.log('The array after implementing pop() - ',arr_name);
//output - The array after pop() -  [ 'val1', 'val2', 'val3', 'val4' ]

Shift()

This method is similar to pop(), the only difference is shift() removes the first element and shifts the remaining value to one index behind. It returns the shifted value i.e. the first value.

Let's see the syntax -

let arr_name = ['val1','val2','val3','val4'];
console.log('The shifted value is ',arr_name.shift());
//output - The shifted value is  val1
console.log('The array after implementing shift() - ',arr_name);
//output - The array after implementing shift() -  [ 'val2', 'val3', 'val4' ]

Unshift()

This method adds a given value at the start of an array hence shifting the remaining elements to one index later.

ex -

let arr_name = ['val1','val2','val3','val4'];
arr_name.unshift('first_val');
console.log('The array after implementing unshift() - ',arr_name);
//output - The array after implementing unshift() -  [ 'first_val', 'val1', 'val2', 'val3', 'val4' ]

lastIndexOf()

This method returns the last index of value present in an array. If the value is present only once, then it returns its index, if it is present multiple times, then it returns the last index.

Let's see the example -

let arr_name = ['val1','val2','val3','val1','val1','val4'];
console.log('The last index of value - "val1" is ',arr_name.lastIndexOf('val1'));
//output - The last index of value - "val1" is  4

Map()

If we want to run some function on each element of the array then map() is useful. This method executes some operation on each member of the array.Let's understand it by example -

let numbers = [2,3,4,5,9,16];
console.log(numbers.map(Math.sqrt));
//output - [ 1.4142135623730951, 1.7320508075688772, 2, 2.23606797749979, 3, 4 ]

in the above example, it executes Math.sqrt on every element of the array.

Reverse()

This method simply changes the order in which elements are present in the array and also returns the modified array.

ex -

let arr_name = ['val1','val2','val3','val4'];
arr_name.reverse();
console.log('Array after reverse() - ',arr_name);
//output - Array after reverse() -  [ 'val4', 'val3', 'val2', 'val1' ]

Split()

This method is used to convert a String into an array. As the name suggests, it splits alphabets of string into array elements. let's see the syntax -

let name = "Hello";
let arr = name.split('');
console.log(arr);
//output - [ 'H', 'e', 'l', 'l', 'o' ]

Hello Guys

So this article end here. In this article, I have tried to explain the most used methods of array manipulation in Javascript. Please comment for any suggestions, improvements or feedback. Happy Learning....!! Thank you.