Which one method use to delete an element from JavaScript array (slice or delete) ?
Last Updated :
28 Oct, 2021
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer.
What slice() method does?
The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We can give this copy of the array to the main array or to any other array to get the sliced array.
Syntax:
array.slice(arg1, arg2);
Parameter: It accepts the following two parameters:
- arg1: It refers to the start index from which the method will begin slicing, the start index is inclusive which means if we give 0, it will begin with the 0th index.
- arg2: It refers to the end index. This end index is exclusive which means it will not include the given index in slicing
It is not necessary to give both arguments, if we do not provide the start index, it will start with the 0th index and if we do not provide the end index it will take to the last index.
Return Value: It returns a copy of the new array
Example 1: Here, we are giving the start index as 2 and end index as 4, so it will slice the elements 2, 3. As you can see it returns the copy of the new array, which we are storing in the same array. This method also shortens the array length. Previously array had a length of 4, after slicing the array, it became 2.
JavaScript
<script>
// Sample array
var arr = ["super", "duper", "upar", "chopper"];
// Printing array length before function call
console.log(arr.length);
// Function call
arr = arr.slice(2, 4);
// Printing array and its length
// after function call
console.log(arr, arr.length);
</script>
Output:
4
[ 'upar', 'chopper' ] 2
Example 2: Here we are not giving the end index so it will begin with the 1st index and slice till last.
JavaScript
<script>
// Sample array
var arr = ["super", "duper", "upar", "chopper"];
// Printing length before function call
console.log(arr.length);
// Function call
arr = arr.slice(1);
// Printing array and its length
// after function call
console.log(arr, arr.length);
</script>
Output:
4
[ 'duper', 'upar', 'chopper' ] 3
What delete() method does?
The delete is more like an operator than the method. It is used to remove the property from the object, it can also remove the data from the array. But it does not remove the references completely which means it does not shorten the array. The delete operator requires the reference to remove the property from that reference.
Example 1: As we can see, we have given the delete operator a reference arr[1] to be removed. But it only removes the property which it was holding. The reference is still there and the length is still 4, it did not shorten our array.
JavaScript
<script>
// Sample array
var arr = ["super", "duper", "upar", "chopper"];
// Printing array length before delete call
console.log(arr.length);
// Deleting
delete arr[1];
// Printing array and its length
// after delete call
console.log(arr, arr.length);
</script>
Output:
4
[ 'super', <1 empty item>, 'upar', 'chopper' ] 4
Example 2: In this second example. arr[2] reference is still there, only its value is removed, hence the undefined is shown, these deleted memory will be released automatically when there is no reference to the arr is left.
JavaScript
<script>
// Sample array
var arr = ["super", "duper", "upar", "chopper"];
// Printing array length before delete call
console.log(arr.length);
// Deleting
delete arr[2];
// Printing array reference
console.log("The reference arr[2] is still there :", arr[2]);
// Printing array length after delete call
console.log(arr, arr.length);
</script>
Output:
4
The reference arr[2] is still there : undefined
[ 'super', 'duper', <1 empty item>, 'chopper' ] 4
Conclusion: If we want to shorten the length of the array, we will use the slice method and if we want to keep the array length the same for some reason, we will use the delete operator.
References:
Similar Reads
Delete the first element of array without using shift() method in JavaScript Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript. There are two approaches that are discussed below: Table of
2 min read
Which built-in method removes the last element from an array and returns that element in JavaScript ? In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding its implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We
3 min read
How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
5 min read
JavaScript - Delete Elements from the End of a JS Array These are the following ways to delete elements from the end of JavaScript arrays:1. Using pop() methodThe pop() method is used to remove the last element from an array and returns the modified array.JavaScriptlet a = [1, 2, 3, 4]; // Remove last element a.pop(); console.log(a);Output[ 1, 2, 3 ] 2.
2 min read
How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met
2 min read
JavaScript - Delete Element from the Beginning of JS Array These are the following ways to delete elements from JavaScript arrays:1. Using shift() method - Mostly UsedThe shift() method removes the first element from the array and adjusts its length.JavaScriptconst a = [1, 2, 3, 4]; // Remove the first element a.shift(); console.log(a);Output[ 2, 3, 4 ] 2.
2 min read
JavaScript - Delete Elements from an Index in JS Array These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using splice() MethodThe splice() method is used to remove elements directly from a specified index in an array by specifying the index and the number of elements to delete.JavaScriptlet a = [1, 2, 3, 4, 5
1 min read
How to Remove First and Last Element from Array using JavaScript? Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation. Example:Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]Removing first and last element in array can be do
2 min read
JavaScript - Delete First Occurence of Given Element from a JS Array These are the following ways to delete elements from a specified Index of JavaScript arrays:1. Using indexOf() and splice() - Most UsedThe indexOf() method finds the first occurrence of the element in the array, and splice() removes it at that index. This approach directly modifies the original arra
2 min read
How to move an array element from one array position to another in JavaScript? In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another
5 min read