A summary of the JavaScript arrays (array) method

Source: Internet
Author: User

Array Object Properties
Properties Description
Constructor Returns a reference to the array function that created this object.
Length Sets or returns the number of elements in the array.
Prototype gives you the ability to add properties and methods to an object.
Array Object method
Method Description
Concat () Joins two or more arrays and returns the result.
Join () Put all the elements of the array into a string. element is delimited by the specified delimiter.
Pop () Delete and return the last element of the array
Push () Adds one or more elements to the end of the array and returns the new length.
Reverse () Reverses the order of the elements in the array.
Shift () Delete and return the first element of the array
Slice () Returns the selected element from an existing array
Sort () Sorting elements of an array
Splice () Deletes the element and adds a new element to the array.
Tosource () Returns the source code for the object.
ToString () Converts the array to a string and returns the result.
toLocaleString () Converts the array to a local array and returns the result.
Unshift () Adds one or more elements to the beginning of the array and returns the new length.
ValueOf () Returns the original value of an array object
Summarize:shift (): The method removes the first element of the array, minus 1 of the array length, and returns the value of the element it was removed from. If the array is already empty, shift () does not change the array and returns undefined.  pop (): The method deletes the last element of the array, minus 1 of the array length, and returns the value of the element it was removed from. If the array is already empty, then pop () does not change the array and returns undefined.   push (): The method can add its parameter order to the end of the array. Instead of creating a new array, it modifies the array directly. Returns the length of the new array.  unshift (): The method can add its parameter order to the head of the array. Instead of creating a new array, it modifies the array directly. Returns the length of the new array.unshift () Ie6,ie7, the data has been added successfully, but the return value is undefined1. The shift () method is used to remove the first element in the array and return the value of the first element. Syntax: Arrayobject.shift () return value:The value of the original first element of the array。 Description: If the array is empty, the shift () method does nothing and returns undefined. Note that the method does not create a new array, but instead modifies the original array directly. The method changes the length of the array. Tip: To delete and return the last element of the array, use the Pop () method. Example: var arr = new Array (3); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; document.write (arr + "
") document.write (Arr.shift () +"
") document.write (arr) output: George,john,thomas George john,thomas The 2.pop () method is used to delete and return the last element of the array. Syntax: Arrayobject.pop () return value:the value of the last element of the array. Description: The Pop () method removes the last element of arrayobject, reducing the length of the array by 1, and returning the value of the element it deleted.  If the array is already empty, pop () does not change the array and returns the undefined value. Example: var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write (arr) document.write (Arr.pop ( )) document.write (arr) output: George,john,thomas Thomas George,john 3. The push () method adds one or more elements to the end of the array and returns the new length. Syntax: Arrayobject.push (newelement1,newelement2,...., newelementx) parameter description: Newelement1 required.     The first element to add to the array. Newelement2 is optional.     The second element to add to the array. Newelementx is optional. You can add multiple elements. return value:returns the length of the new array. Description: The push () method adds its arguments sequentially to the end of the original array. Instead of creating a new array, it modifies the original array directly. The method changes the length of the array.   The push () method and the Pop () method use the advanced post-stack functionality provided by the array. Example: var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write (arr + "
") document.write (Arr.push (" James ") +"
") document.write (arr) output: George,john,thomas 4 george,john,thomas,james

A summary of the JavaScript arrays (array) method

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.