I know how to initliaize one but how do add I items to an Array? I heard it was push()
maybe? I can't find it...
When you want to add an element to the end of your array, use push(). If you need to add an element to the beginning of your array, try unshift(). And you can add arrays together using concat().
JavaScript Array push() The push() method adds new items to the end of an array. The push() method changes the length of the array.
To append one array to another, call the concat() method on the first array, passing it the second array as a parameter, e.g. const arr3 = arr1. concat(arr2) . The concat method will merge the two arrays and will return a new array.
For JavaScript arrays, you use push()
.
var a = []; a.push(12); a.push(32);
For jQuery objects, there's add()
.
$('div.test').add('p.blue');
Note that while push()
modifies the original array in-place, add()
returns a new jQuery object, it does not modify the original one.
push is a native javascript method. You could use it like this:
var array = [1, 2, 3]; array.push(4); // array now is [1, 2, 3, 4] array.push(5, 6, 7); // array now is [1, 2, 3, 4, 5, 6, 7]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With