Jquery USES push of to add elements to an array of of

  • 2020-03-30 04:23:02
  • OfStack

Push definition and usage

The push() method adds one or more elements to the end of the array and returns a new length.

grammar

ArrayObject. Push (newelement1, newelement2,... , newelementX)

Parameters to describe
Newelement1 required. The first element to add to the array.
Newelement2 optional. To add to the second element of the array.
NewelementX optional. Multiple elements can be added.


The return value

Adds the specified value to the new length of the array.

instructions

The push() method adds its argument order to the end of the arrayObject. Instead of creating a new array, it simply modifies the arrayObject. The push() and pop() methods use the advanced post-stack functionality provided by arrays.

Hints and comments

Note: this method changes the length of the array.
Tip: to add one or more elements to the beginning of an array, use the unshift() method.

The instance

In this example, we will create an array and change its length by adding an element:


var arr = new Array(3);
arr[0] = "George" ;
arr[1] = "John" ;
arr[2] = "Thomas" ;
document.write(arr + "") document.write(arr.push("James")+ "") document.write(arr);

Output:
George, John Thomas
4
George, John, Thomas, James

An online usage:


$(function(){
 var buf = [];
 buf.push('<script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>');
 buf.push('<ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-88888888" data-ad-slot="8305246055"></ins>');
 buf.push('<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>');
 $('.footer-banner').html(buf.join(''));

Note: jquery.js needs to be loaded first


Related articles: