1.

Convert a Date object to String in JavaScript?

Answer»

To remove the last element from an array in JavaScript, use the pop() method.<BR>
The following is the syntax:

array.pop()

Let’s say we have the following array:

var arr = [20, 11, 44, 39, 88, 48, 89, 47 ];

To remove an element, use the pop() method:

var element = arr.pop();

Let us see an example wherein we are removing 5 elements:

The method returns the REMOVED element from the array. In the below example, we have an array and we have removed the last element from the array. With that we have removed 4 more elements from the end one by one using the pop() method:

<!DOCTYPE html> <html> <body> <SCRIPT> var arr = [20, 11, 44, 39, 88, 48, 89, 47 ]; document.write("Array = "+arr);   document.write("<br><br>Removing 5 elements...<br>");         var element = arr.pop(); document.write("Popped Element = " + element ); var element = arr.pop(); document.write("<br />Popped Element = " + element ); var element = arr.pop(); document.write("<br />Popped Element = " + element ); var element = arr.pop(); document.write("<br />Popped Element = " + element ); var element = arr.pop(); document.write("<br />Popped Element = " + element ); </script> </body> </html>

The OUTPUT:

Array = 20,11,44,39,88,48,89,47 Removing 5 elements... Popped Element = 47 Popped Element = 89 Popped Element = 48 Popped Element = 88 Popped Element = 39


Discussion

No Comment Found