How to Remove Last Element from an Array in JavaScript? 5 Ways

Are you thrilled to know the different ways to remove the last element from an array in JavaScript?

Discover five (5) different ways to remove the last element from an array.

This article covers all the methods you need to know. From using the pop() method to manipulating the length property.

Aside from that, you’ll also learn how to remove the last two (2) or multiple elements from an array in JavaScript.

JavaScript remove last element from array

Here are 5 different ways to remove the last element from an array in JavaScript:

Using the pop() method

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Here’s an example:


let subjects = ["Software Engineering", "Math", "Programming", "Web Development", "Software Development"];
let lastElement = subjects.pop(); ✅
console.log(subjects);

Output:

[ 'Software Engineering', 'Math', 'Programming', 'Web Development' ]

Using the splice() method

The splice() method can be used to alter the contents of an array by removing, replacing, or adding elements.

To remove the last element of an array, you can call splice(-1, 1) on the array.

Here’s an example:

let subjects = ["Software Engineering", "Software Development", "Programming", "Web Development", "Math"];
subjects.splice(-1, 1); 
console.log(subjects); 

Output:

[
  'Software Engineering',
  'Software Development',
  'Programming',
  'Web Development'
]

Using the slice() method

The slice() method creates a new array object that contains a shallow copy of a portion of an existing array.

The portion is specified by the start and end indices, where the start index is included and the end index is not.

To remove the last element, you can use slice(0, -1).

Here’s an example:

let subjects = ["Software Engineering", "Math", "Software Development", "Web Development", "Programming"];
subjects = subjects.slice(0, -1); 
console.log(subjects);

Output:

[
  'Software Engineering',
  'Math',
  'Software Development',
  'Web Development'
]

Using the length property

You can also remove the last element from an array by setting its length property to one less than its current value.

Here’s an example:

let subjects = ["Software Development", "Math", "Programming", "Web Development", "Software Engineering"];
subjects.length -= 1; 
console.log(subjects); 

Output:

[ 'Software Development', 'Math', 'Programming', 'Web Development' ]

Using the delete operator

The delete operator is used to cut or remove a property from an object. You can use it to remove the last element from an array by deleting the property at the index of the last element.

let subjects = ["Software Engineering", "Math", "Programming", "Software Development", "Web Development"];
delete subjects[subjects.length - 1]; 
console.log(subjects);

Output:

[
  'Software Engineering',
  'Math',
  'Programming',
  'Software Development',
  <1 empty item>
]

When you use it to remove the last element from an array by deleting the property at the index of the last element, it leaves an empty slot in the array.

This is why the output shows <1 empty item> at the end of the array.

If you want to remove the last element from an array without leaving an empty slot, you can use one of the other methods which we mentioned earlier, such as pop(), splice(), slice(), or setting the length property to one less than its current value.

How to remove last 2 or 3 element from array in JavaScript?

The following are a few ways to remove the last 3 elements from an array in JavaScript.

To remove the last 3 elements, you can use splice(-3, 3)

Here’s an example:

let subjects = ["Software Engineering", "Math", "Programming", "Web Development", "Software Development"];
subjects.splice(-2,2 ); ✅
console.log(subjects); 

Output:

[ 'Software Engineering', 'Math', 'Programming' ]

Here’s another example:

let subjects = ["Software Engineering", "Math", "Programming", "Web Development", "Software Development"];
subjects.splice(-3, 3); 
console.log(subjects); 

Output:

[ 'Software Engineering', 'Math' ]

To remove the last 3 elements, you can use slice(0, -3)

Here’s an example:

let subjects = ["Web Development", "Software Development", "Software Engineering", "Math", "Programming"];
subjects = subjects.slice(0, -3); 
console.log(subjects); 

Output:

[ 'Web Development', 'Software Development' ]

You can also remove the last 3 elements from an array by setting its length property to three less than its current value

Here’s an example:

let subjects = ["Programming", "Web Development", "Software Engineering","Software Development", "Math"];
subjects.length -= 3; 

console.log(subjects); 

Output:

[ 'Programming', 'Web Development' ]

How to remove 1st and last element in array JavaScript?

Here are the 3 different ways to remove the first and last elements from an array in JavaScript.

Using the shift() and pop() methods

The shift() method is used to remove and return the first element of an array.

The pop() method removes the last element from an array and returns that element. You can use both methods to remove the first and last elements from an array.

Here’s an example:

let subjects = ["Software Engineering", "Math", "Programming", "Web Development", "Software Development"];
let firstElement = subjects.shift(); ✅
let lastElement = subjects.pop(); 
console.log(subjects); 

Output:

[ 'Math', 'Programming', 'Web Development' ]

Using the splice() method

To remove the first and last elements, you can use splice(0, 1) to remove the first element and splice(-1, 1) to remove the last element.

Here’s an example:

let subjects = ["Programming", "Web Development", "Software Development", "Math", "Software Engineering"];
subjects.splice(0, 1); 
subjects.splice(-1, 1); 
console.log(subjects);

Output:

[ 'Web Development', 'Software Development', 'Math' ]

Using the slice() method

To remove the first and last elements, you can use slice(1, -1).

Here’s an example:

let subjects = [ "Math", "Programming", "Web Development", "Software Development", "Software Engineering"];
subjects = subjects.slice(1, -1); 
console.log(subjects);

Output:

[ 'Programming', 'Web Development', 'Software Development' ]

Conclusion

In conclusion, there are several ways to remove the last element from an array in JavaScript.

This article has covered five different methods, including using the pop(), splice(), slice(), and delete methods, as well as manipulating the length property.

Additionally, you can also use these methods to remove multiple elements from the end of an array.

We are hoping that this article provides you with enough information that help you understand the JavaScript remove last element from array.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment