- shift() method: Removes the first element from an array and returns that element.
- splice() Method: Remove any item in an Array based on Index Value:
HTML example code.
Note: The splice() method changes the contents of an array by removing existing elements.
Remove the first 2 elements from Array.
<!DOCTYPE html> <html> <body> <script> var colors = ["red","blue","black","green"]; var indexToRemove = 0; var numberToRemove = 2; colors.splice(indexToRemove, numberToRemove); console.log(colors); </script> </body> </html>
Do comment if you have any doubts or suggestions on this JS Array code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version