JavaScript array---push (), concat () method Difference __java

Source: Internet
Author: User
Tags javascript array

In array operations, push () is common, concat () is very rare, but the use of the two is very similar, it can be understood that push () is a simplified version of Concat (), first look at the following example:

    /*push () method * *
    var array=[1,2,3,4,5];

    Console.log (array);   [1, 2, 3, 4, 5]

    Array.push (6);        A parameter
    console.log (array);   [1, 2, 3, 4, 5, 6]

    Array.push (6,7);      Two parameters
    Console.log (array);   [1, 2, 3, 4, 5, 6, 7]

    array.push ([6,7]);    Parameters are array
    console.log (array);   [1, 2, 3, 4, 5, 6, Array (2)]
    /*concat () method * *
    var array=[1,2,3,4,5];

    Console.log (array);   [1, 2, 3, 4, 5]

    var array2=array.concat (6);    A parameter
    console.log (array);    [1, 2, 3, 4, 5]
    console.log (array2);   [1, 2, 3, 4, 5, 6]

    var array2=array.concat (6,7);    Two parameters
    Console.log (array);    [1, 2, 3, 4, 5]
    console.log (array2);   [1, 2, 3, 4, 5, 6,7]

    var array2=array.concat ([6,7]);    Parameters are array
    console.log (array);    [1, 2, 3, 4, 5]
    console.log (array2);   [1, 2, 3, 4, 5, 6, 7]

A few differences can be seen through the code:
The 1,push () is modified on the basis of the original array, and the value of the original array is changed after the push () method is executed; Concat () first copies the original array to a new array and then operates on the new array, so it does not change the value of the original array.

2, if the parameter is not an array, regardless of the number of parameters, push () and concat () add parameters directly to the array, and if the argument is an array, push () adds the array directly to the original array, and concat () takes the value out of the array and adds it to the original array.

Related Article

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.