JavaScript array push

 The JavaScript push method can add a new array element to the back of the array object and return the new array length. The new elements are added sequentially at the end of the array. If you want to add it to the front of the array Elements should be processed using the JavaScript unshift method. The JavaScript array push method is also a built-in JavaScript function, which can be called at any time without additional installation, and almost all mainstream browsers support the JavaScript push method.


JavaScript array push basic syntax

array.push('item1','item2','item3','item4', ...)


The array object is followed by push to add elements to the back of the array. Item is a necessary item. At least one is required. There is no special restriction at most. Although the JavaScript array push technique does not limit the array elements that can be added Quantity, but you should also try to avoid endless push and cause the browser to be unable to process.

JavaScript array push example
<script type="text/javascript">
var Arr = ['Father','Mother','Brother'];

document.write('Array content:'+Arr+'<br>');
document.write(' Array length:'+Arr.length+'<p>');

Arr.push('Sister');

document.write('Array content:'+Arr+'<br>');
document.write('Array length: '+Arr.length+'<p>');
</script>
The output result of the above JavaScript array push example

Array content: Father,Mother,Brother
Array length: 3

Array content: Father,Mother,Brother,Sister
Array length: 4

At the beginning of the example, an array Arr is prepared. There are three array values, namely Father, Mother, Brother. The original array result is output through document.write . For the length of the array, we use the JavaScript length function, and then use Arr. The push method hangs the new array value Sister to the end of the array Arr, and re-outputs the array to see the result. There are more elements of Sister and the length increased to 4, which represents the successful operation of JavaScript array push.

Post a Comment

0 Comments