Removing an element from a given position of the array in Javascript


In this article, we are going to discuss removing an element from a given position of the array in JavaScript.

The delete operator, the slice() method, and the splice() method can be used to remove an element from a given position of the array in this article.

Following is the syntax of the delete operator.

let del = delete arr[index];

Following is the syntax of the slice() method.

arr.slice(start, end)

Following is the syntax of the splice() method.

arr.splice(position, no_of_elem)

Using delete Operator

Example

In this example, we are going to discuss the use of the delete operator. Here, we will use the delete operator to remove the element from a given position of the array, so that it will exclude the given indexed element of an array.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Remove an element using delete operator</title>
      <div id="remove"></div>
   </head>
   <body>
      <script>
         let arr = [10, "Alice", 20, "Edward", 30, 40];
         var remove = delete arr[3];
         document.getElementById("remove").innerHTML =
         "The array elements after using the delete operator is : " + arr;
      </script>
   </body>
</html>

Using slice() Method

Example

In this example, we are going to discuss the use of the slice() method. We will use the slice() method to remove the element from a given position of the array so that it will exclude the given indexed element of an array.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>
      <title>Remove an element using slice() method</title>
      <div id="original"></div>
      <div id="slice"></div>
   </head>
   <body>
      <script>
         let arr = [10, "Alice", 20, "Edward", 30, 40];
         let remove_elem_index = 4;
         document.getElementById("original").innerHTML ="The array elements are : " + arr;
         let sliceArr = [
            ...arr.slice(0, remove_elem_index),
            ...arr.slice(remove_elem_index + 1),
         ];
         document.getElementById("slice").innerHTML ="The array elements after using the slice() method : " + sliceArr;
      </script>
   </body>
</html>

Using splice() Method

Example

In this example, we are going to discuss the use of the splice() method. We will use the splice() method to remove the element from a given position of the array so that it will exclude the given indexed element of an array.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Remove an element from an array using splice() method</title>
      <div id="original"></div>
      <div id="splice"></div>
   </head>
   <body>
      <script>
         let arr = [10, "Alice", 20, "Edward", 30, 40];
         document.getElementById("original").innerHTML ="The array elements are : " + arr;
         let del = arr.splice(4, 1);
         document.getElementById("splice").innerHTML ="The array elements after using the splice() method : " + arr;
      </script>
   </body>
</html>

Using splice() Method

Example

JavaScript gives the splice method to remove from a given index. It can be used as follows −

let veggies = ["Onion", "Raddish", "Broccoli"];
veggies.splice(0, 1); // Removes 1 element from index 0
console.log(veggies);

Output

This will give the output −

["Raddish", "Broccoli"]

Updated on: 07-Dec-2022

136 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements