Push Function:
Adds one or more elements to the end of an array and returns the last element added to the array. This method changes the length of the array.
Syntax:
arrayname.push("element1","element2",.......,"elementN")
Description:
The behavior of the push method is analogous to the push function in Perl 4. Note that this behavior is different in Perl 5.
Example in HEAD Section:
<html> <head> <title>Push Function</title> <script language="javascript"> myfish=new Array("Elephent", "Lion" , "Tiger" , "Cat" , "Dog"); document.write("<b>Before my Array is:</b> " +myfish +"<br>"); document.write("<b>Total element in the Array is:</b> " + myfish1.push("Mouse")); document.write("<br><b>After my Array is:</b> " +myfish); </script> </head> </html> |
Example in BODY Section:
<html> <head> <title>Push Function</title> </head> <body> <script language="javascript"> myfish=new Array("Elephent", "Lion" , "Tiger" , "Cat" , "Dog"); document.write("<b>Before my Array is:</b> " +myfish +"<br>"); document.write("<b>Total element in the Array is:</b> " + myfish1.push("Mouse")); document.write("<br><b>After my Array is:</b> " +myfish); </script> </body> </html> |
OUTPUT:
Before my Array is: Elephent,Lion,Tiger,Cat,Dog
Total element in the Array is: 6
After my Array is: Elephent,Lion,Tiger,Cat,Dog,Mouse