Remove The Last Element From An Array In JavaScript

To remove the last element from an array in JavaScript, you can use any of the following methods –

  1. Use the Array.pop() method: The Array.pop() method removes the last element from an array and returns that element. This method changes the length of the array. Example: var arr = [‘a’, ‘b’, ‘c’]; arr.pop(); // removes ‘c’ (the last element)
  2. Use the Array.splice() method: The Array.splice() method removes elements from an array and returns the removed elements. This method changes the length of the array. Example: var arr = [‘a’, ‘b’, ‘c’]; arr.splice(-1,1); // removes ‘c’ (the last element)
  3. Use the Array.slice() method: The Array.slice() method returns a shallow copy of a portion of an array. This method does not change the original array. Example: var arr = [‘a’, ‘b’, ‘c’]; arr.slice(0, -1); // removes ‘c’ (the last element)

Let’s discuss each of the above methods in detail.

Remove The Last Element From An Array In JavaScript

remove the last element from an array in javascript

pop() Method

The Array.pop() method is used to remove the last element from an array in JavaScript and return that element. This method changes the length of the array.

Syntax: arr.pop();

Return value: Returns the removed element from the array. If the array is empty, it returns undefined.

Example:

var arr = [‘a’, ‘b’, ‘c’];

console.log(arr.pop()); // outputs ‘c’

console.log(arr); // outputs [‘a’, ‘b’]

As you can see from the above example, the Array.pop() method removes the last element (‘c’) from the array and returns it. This method also changes the length of the array (from 3 to 2).

If the pop() method is called on an empty array, it returns undefined.

splice() Method

The Array.splice() method is used to remove one or more elements from an array and return the removed elements. This method changes the length of the array.

Syntax: arr.splice(index, deleteCount);

Parameters:

index: The index at which to start changing the array.

deleteCount (optional): The number of elements to remove. If this parameter is omitted, all elements from the start index to the end of the array will be removed.

Return value: Returns an array containing the removed elements. If no elements are removed, it returns an empty array.

Example:

var arr = [‘a’, ‘b’, ‘c’];

console.log(arr.splice(-1,1)); // outputs [‘c’]

console.log(arr); // outputs [‘a’, ‘b’]

As you can see from the above example, the Array.splice() method removes the last element (‘c’) from the array and returns it in an array. This method also changes the length of the array (from 3 to 2).

slice() Method

The Array.slice() method is used to return a shallow copy of a portion of an array. This method does not change the original array.

Syntax: arr.slice(start, end);

Parameters:

start (optional): The index at which to start the slice. If this parameter is omitted, the slice starts from the beginning of the array.

end (optional): The index at which to end the slice. If this parameter is omitted, the slice ends at the end of the array.

Return value: Returns a new array containing the extracted elements.

Example:

var arr = [‘a’, ‘b’, ‘c’];

console.log(arr.slice(0, -1)); // outputs [‘a’, ‘b’]

console.log(arr); // outputs [‘a’, ‘b’, ‘c’]

As you can see from the above example, the Array.slice() method returns a new array containing the extracted elements (‘a’ and ‘b’), but the original array is unchanged.

Conclusion

In conclusion, the Array.pop() method can be used to remove the last element from an array in JavaScript and returns that element. The Array.splice() method removes one or more elements from an array and returns the removed elements. The Array.slice() method returns a shallow copy of a portion of an array.

I hope you found this article helpful. If you have any questions or comments, please feel free to post them in the comments section below.

Happy coding! 🙂

Leave a Reply