Push () method

Time:2021-1-8

effect:

Adds one or more elements to the end of the array and returns the new length

Grammar:

array.push(item1, item2, …, itemX)

  • Item, the element added to the array

be careful:

The new element is added at the end of the array
This method changes the length of the array
This method does not generate a new array~~~~

Example: Es5

<script>

  var fruits = ["Banana", "Orange", "Apple", "Mango"];
    console.log(fruits.push("Kiwi","Lemon","Pineapple"))
    //Output: banana, orange, apple, mango, kiwi, lemon, pineapple
    
</script>

Example: ES6

<script>

  var arr = ['tom', 'jerry'];
  var arr2 = [1, 2];
  //Point Grammar: extension budget operator
  arr.push(...arr2);
  console.log(arr) 
  //Output: ["Tom", "Jerry", 1, 2]
  
</script>