
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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"]
- Related Articles
- Removing an element from an Array in Javascript
- Removing an element from the end of the array in Javascript
- Removing an element from the start of the array in javascript
- Adding an element at a given position of the array in Javascript
- Removing the odd occurrence of any number/element from an array in JavaScript
- Removing an array element from a MongoDB collection
- Completely removing duplicate items from an array in JavaScript
- Removing Negatives from Array in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Removing comments from array of string in JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Removing an array element from MongoDB collection using update() and $pull
- JavaScript Algorithm - Removing Negatives from the Array
- Removing duplicate objects from array in JavaScript
- Removing all the empty indices from array in JavaScript
