What is the difference between
<cfscript>
i = []
i.push(1)
i = []
i.append(1)
</cfscript>
?
They both seem to have the same results.
What is the difference between
<cfscript>
i = []
i.push(1)
i = []
i.append(1)
</cfscript>
?
They both seem to have the same results.
In addition to James A. Mohler's answer where the return value is different for each function, there's another distinction between the two. For
append()
, there's also an additional optional boolean parametermerge
which if set totrue
(default) will merge to the source array. If false, it will add the array as an additional element at the end. For your example of appending a single element to the array, setting themerge
parameter to either true or false changes nothing. However, if you're appending 2 arrays together, the difference is clear. For exampleEDIT (from James A. Mohler's comment)
Results
i.append([6,7], true);
[1,2,3,4,5,6,7]i.append([6,7], false);
[1,2,3,4,5,[6,7]]i.push([6,7]);
[1,2,3,4,5,[6,7]]You can see the gist here.