Basic Array Methods in Javascript

Basic Array Methods in Javascript

In this blog, we will explore the basic array methods in Javascript.

·

4 min read

In a previous blog, we have seen the essential introduction to array on how to create and access array elements and in this blog, we are going to explore basic array methods in javascript.

1. Length Property:

Before moving to methods in the array, we have a property to find the length of the array(i.e., how many elements it consists of).

Let us understand by an example,

let arr = ["Apple", "Orange", "Banana"];
console.log(arr.length); //3

In the above example, we are creating an array with 3 elements, and with the help of the length property we can find out how many elements are there in the array. In this example, we have 3 elements inside the array and we got that as an output.

2. tostring():

tostring() function is used to convert the array into a string with separated by a comma.

Example:

let arr = ["Apple", "Orange", "Banana"];
console.log(arr.toString()); //Apple,Orange,Banana

3. join():

join() function is also used to convert the array into a string. By default in toString() it will use the comma separator to separate the string elements while converting it. Likewise, in join() the change is, that we will specify the separator as a parameter inside the join() function within double quotes(i.e., which separator we are going to use).

Let us understand by an example,

let arr = ["Apple", "Orange", "Banana"];
console.log(arr.join("|"));

In the above example, we are converting an array into a string and then we are joining the string elements using the separator(i.e., | ). We can use any separator as a parameter to join the string elements.

4. How to add and remove an element:

To add and remove elements from the array we will use the push() function and pop() function.

push() method:

To add an element to an array, we use the push() function.

Let us understand by an example,

let arr = ["Apple", "Orange", "Banana"];
arr.push("Grapes");
console.log(arr); //[ 'Apple', 'Orange', 'Banana', 'Grapes' ]

In the above example, the push() function will add the new element at the end of the array and make the changes in the existing array and display the output.

pop() method:

To remove an element from the array, we use the pop() function.

Let us understand by an example,

let arr = ["Apple", "Orange", "Banana"];
arr.pop();
console.log(arr); //[ 'Apple', 'Orange', 'Banana']

In the above example, pop() will remove the last element from the array and make the changes in the existing array and display the output.

5. shift() and unshift():

shift() and unshift() function is used to add and remove array elements from the beginning of the array.

unshift() method:

To add an element at the beginning of the array, we use the unshift() method.

Example:

let arr = ["Apple", "Orange", "Banana"];
arr.unshift("Grapes");
console.log(arr); //[ 'Grapes', 'Apple', 'Orange', 'Banana' ]

shift() method:

To remove an element at the beginning of the array, we use the shift() method.

Example:

let arr = ["Apple", "Orange", "Banana"];
arr.shift();
console.log(arr); //[ 'Orange', 'Banana' ]

6. slice():

The Slice() method is used to slice the array of elements. It has two parameters, are

  • The first parameter is the starting index value.
  • The second parameter is the ending index value.

Example:

let arr = ["Apple", "Orange", "Banana", "Grapes"];
output_arr = arr.slice(1,3);
console.log(output_arr); //[ 'Orange', 'Banana' ]

In the above example, the slice() method will slice the array of elements. Array elements are sliced by their index value. The first parameter index value is included and the second parameter index value is excluded(i.e., it represents the index value to slice the array).

7. splice():

The splice() method is also used to replace and remove the element from an array and display that array(i.e., it will not create a separate array for output).

The splice() method has three parameters are

  • The First parameter is the starting index value.

  • The second parameter is the count of the element from the start of the index value.

  • The third parameter is the value we need to replace it with.

Example:

let arr = ["Apple", "Orange", "Banana", "Grapes"];
arr.splice(1,2,"Watermelon");
console.log(arr); //[ 'Apple', 'Watermelon', 'Grapes' ]

In the above example, we are starting with the index value 1 and we are replacing watermelon with the elements that count two elements from the index value 1.

8. concat():

concat() method will concat two arrays.

Example:

let X = ["Apple", "Orange", "Banana", "Grapes"];
let Y = ["Watermelon", "pineapple", "strawberry"];
console.log(X.concat(Y)); //[ 'Apple',  'Orange', 'Banana', 'Grapes',  'Watermelon' 'pineapple',  'strawberry' ]

In the above example, we are concatenating two arrays named X and Y.

In the next blog, we will explore sorting and searching methods in the array.