Remove Element From Array by Index in JavaScript

Remove Element From Array by Index in JavaScript | A single variable called a JavaScript array is used to store a collection of data or items. The array can have elements added or removed at any point. This article will cover the many approaches to eliminating elements from the array. The following methods are just a few of the many that can be used to delete elements from a JavaScript array:-

  • The slice() function can be used to remove elements from any given index position.
  • The pop() function in JavaScript is used to remove elements from an array’s tail.
  • The shift() function in JavaScript is used to remove elements from the beginning of an array.

The result of the Javascript arr.slice() method is a new array that contains a subset of the original array. The initial is unaltered.

Remove the Last Element From Array in JavaScript

The pop() method returns the removed element after removing the last element from the array. By using this function, the array’s length is cut by 1.

var arr = ["Java", "C++", "Python", "HTML"];
// Popping the last element from the array
var popped = arr.pop();
console.log("Removed element: " + popped);
console.log("Remaining elements: " + arr);

Output:-

Removed element: HTML
Remaining elements: Java, C++, Python

Using slice() function the same thing can be done as follows. Note that the original array isn’t modified instead slice() method returns a new array.

const arr = ["Java", "C++", "Python", "HTML"];
const newArr = arr.slice(0, -1);
console.log("New Array: " + newArr);

Output:-

New Array: Java, C++, Python

Remove the First Element From Array in JavaScript

Using the shift() function, the array’s initial element is eliminated, bringing the array’s size down by one.

var arr = ["Java", "C++", "Python", "HTML"];
// Popping the last element from the array
var shifted = arr.shift();
console.log("Removed element: " + shifted);
console.log("Remaining elements: " + arr);

Output:-

Removed element: Java
Remaining elements: C++, Python, HTML

The same thing can be done through the slice() function as follows:-

const arr = ["Java", "C++", "Python", "HTML"];
const newArr = arr.slice(1);
console.log("New Array: " + newArr);

Output:-

Remove Element From Array by Index in JavaScript Using slice() & concat

function removeAt(array, index) {
  return array.slice(0, index).concat(array.slice(index + 1));
}

const arr = ["Java", "C++", "Python", "HTML", "CSS", "React"];
// remove element at index-2
const newArr = removeAt(arr, 2);
console.log("New Array: " + newArr);

Output:-

New Array: Java, C++, HTML, CSS, React

In the above program, array.slice(0, 2) returns [“Java”, “C++”] and array.slice(3) returns [“HTML”, “CSS”, “React”]. The concat() function joins them together and finally, we get the resultant array after removing the element from the given index.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *