Two ways to empty the JS array

Source: Internet
Author: User

Editor loading ... Mode 1,length assignment to 0 This is a very interesting way,

Other languages, such as Java, have an array whose length is read-only and cannot be assigned a value. such as int[] ary = {1,2,3,4}; ary.length = 0; Java will error, compile pass.

And JS can be, and the array is emptied, var ary = [1,2,3,4]; ary.length = 0; Console.log (ary); Output [], empty array, which is emptied of the current prototype array in the clear and MooTools libraries in the array of empty use this way to empty the array.

Mode 2, the assignment is [] var ary = [1,2,3,4]; ary = []; Assignment to an empty array to clear the original array here is actually not the strict meaning of emptying the array, just ary the value to an empty array, before the array if there is no reference in point to it will wait for garbage collection.

Two ways to empty the JS 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.