
- 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 the start of the array in javascript
In this article, we are going to discuss removing an element from the start of the array in JavaScript.
For that, we are going to use the _.rest() method. This method returns the array elements except for the 0th indexed element. We can also use the shift() method and the slice() method to remove the first element from an array. Let us see the implementation of it further.
Using _.rest() Method
The following example demonstrates how to remove the start element from an array in JavaScript.
Syntax
The syntax of the _.rest() method is −
_.rest( array, index );
Example
In this example, we are going to discuss the use of the _.rest() method. We will use the _.rest() method to remove the first element form an array, so that it will exclude the 0th 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 0th indexed element from an array</title> <div id="original"></div> <div id="rest"></div> </head> <body> <script> let arr = [10, "Alice", 20, "Edward", 30, 40]; document.getElementById("original").innerHTML ="The array elements are : " + arr; document.getElementById("rest").innerHTML ="The array elements after using the _.rest() method : " + _.rest(arr); </script> </body> </html>
Using shift() Method
Example
In this example, we are going to discuss the use of the shift() method. We will use the shift() method to skip the first element from an array, so that it will exclude the 0th 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 0th indexed element from an array</title> <div id="original"></div> <div id="shift"></div> </head> <body> <script> let arr = [10, "Alice", 20, "Edward", 30, 40]; document.getElementById("original").innerHTML ="The array elements are : " + arr; arr.shift(); document.getElementById("shift").innerHTML ="The array elements after using the shift() method : " + arr; </script> </body> </html>
Using shift() Method
Example
Another example to remove an element from the start of the array using the shift() method −
let veggies = ["Onion", "Raddish"]; veggies.shift(); console.log(veggies);
Using slice() Method
Example
In this example, we are going to discuss the use of the slice() method. In this case, we will use the slice() method to skip the first element from an array, so that it will exclude the 0th 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 0th indexed element from an array</title> <div id="original"></div> <div id="slice"></div> </head> <body> <script> let arr = [10, "Alice", 20, "Edward", 30, 40]; document.getElementById("original").innerHTML = "The array elements are : " + arr; let sliceArr = arr.slice(1, arr.length); document.getElementById("slice").innerHTML = "The array elements after using the slice() method : " + sliceArr; </script> </body> </html>
- Related Articles
- Removing an element from the end of the array in Javascript
- Removing an element from an Array in Javascript
- Removing an element from a given position of the array in Javascript
- Removing the odd occurrence of any number/element from an array in JavaScript
- Adding an element at the start of the array in Javascript
- Removing an array element from a MongoDB collection
- Removing 0s from start and end - JavaScript
- Swap certain element from end and start of array - JavaScript
- Completely removing duplicate items from an array in JavaScript
- JavaScript Algorithm - Removing Negatives from the Array
- Removing Negatives from Array in JavaScript
- Removing all the empty indices from array in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Removing comments from array of string in JavaScript
- Removing an array element from MongoDB collection using update() and $pull
