The push method appends values to an array.
push is intentionally generic. This method can be used with call() or apply() on objects resembling arrays. The push method relies on a length property to determine where to start inserting the given values. If the length property cannot be converted into a number, the index used is 0. This includes the possibility of length being nonexistent, in which case length will also be created.
Although strings are native, Array-like objects, they are not suitable in applications of this method, as strings are immutable. Similarly for the native, Array-like object arguments.
The following code creates the sports array containing two elements, then appends two elements to it. The total variable contains the new length of the array.
This example uses apply() to push all elements from a second array.
Do not use this method if the second array (moreVegs in the example) is very large, because the maximum number of parameters that one function can take is limited in practice. See apply() for more details.
// every time an element is added.
// Let's add some empty objects just to illustrate.
Note that although obj is not an array, the method push successfully incremented obj's length property just like if we were dealing with an actual array.