The difference between the concat () and push () methods of a JavaScript reference type array

Source: Internet
Author: User

In JavaScript, we generally only use push to insert new elements into the end of the array, but in fact there is another method in JavaScript, like push, that inserts new elements into the end of the array, but there is a certain difference between them. When we look at the following code, it is obvious that:

1. By using the push operation array:

2. Manipulate an array by using the concat:

from the above two operations it is obvious that the difference between push and concat

When push encounters an array parameter, the entire array parameter is inserted as an object, while the concat is an array parameter, and an element is added to it.

Push changes the current array directly; Concat does not change the current array.

The following code proves the difference above, the code is as follows:

<script>var colors=["Red", "Blue", "green"]; var a={name: "Zhang San"}; var count=// output: 4// output: Red,blue,green,[object Object]colors= Colors.concat (a); Alert (colors[3]); // output: Red,blue,green,[object Object],[object Object] It  is obvious that concat splits a collection into a name object and a "Zhang San" object, while push is a as an object </script>

The difference between the concat () and push () methods of a JavaScript reference type array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.