In JavaScript array there is not any in-built method that will remove the specified element according to the user's choice.
JavaScript array remove
In JavaScript array there is not any in-built method that will remove the specified element according to the user's choice. So we can use the pop() method to remove the element of the array. We can create different methods that will remove the specified element by using the pop() method. Here in the following example we have created a simple remove() method that will remove an array element from the back end side.
In following example code we have created the function remove() which accepts the Array Object name and performs the pop() operation on the passed array object.
function remove(arrayName) { arrayName.pop(); document.writeln("==>Removed one element by the end of Array</br>"); } |
Here is the sample code of JavaScript array remove example as follows:
javascript_array_remove.html
<html> <head> <title> JavaScript array remove example </title> <script type="text/javascript"> var arr = new Array(5); arr[0]="Rose"; arr[1]="India"; arr[2]="Technologies"; arr[3]="Pvt"; arr[4]="Ltd"; function remove(arrayName) { arrayName.pop(); document.writeln("==>Removed one element by the end of Array</br>"); } document.writeln("Array before calling remove() method is =<b>"+arr+"</b></br>"); remove(arr); document.writeln("Array after calling remove() method is =<b>"+arr+"</b></br>"); </script> </head> <body bgcolor="#ddcdff"> <h2> JavaScript array remove </h2> </body> </html> |
Output of the example will look like as shown below :