Javascript array merge push and concat difference analysis _ javascript skills

Source: Internet
Author: User
This article mainly introduces the differences between JS array merge push and concat, and analyzes the differences between the use of push and concat for array merge operations in JavaScript in the form of examples, for more information about the differences between JS array merge push and concat, see the examples in this article. We will share this with you for your reference. The details are as follows:

Note the concat spelling. the two functions are similar, but there are two differences.

First look at the code:

Var arr = []; arr. push (1); arr. push ([2, 3]); arr. push (4, 5); arr = arr. concat (6); arr = arr. concat ([7, 8]); arr = arr. concat (9, 10); arr. each (function (index, value) {alert (value );});

Alert result:

12,345678910

Differences:

When an array parameter is encountered in push, the entire array parameter is used as an element, while concat is used to split the array parameter and add one element to the element.
Push directly changes the current array; concat does not change the current array.

Summary:

If you want to append an array with concat, but it is the same as java's replace, remember to use arr1 = arr1.concat (arr2)

I hope this article will help you design JavaScript programs.

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.