JavaScript Shift () method

Source: Internet
Author: User

http://www.w3school.com.cn/jsref/jsref_shift.aspDefinition and Usage

The shift () method removes the first element from the array and returns the value of the first element.

Grammar
Arrayobject.shift ()
return value

The value of the original first element of the array.

Description

If the array is empty, then the shift () method will not take any action, returning the undefined value. Note that the method does not create a new array, but instead modifies the original arrayobject directly.

Hints and notes

Note: This method will change the length of the array.

Tip: To delete and return the last element of the array, use the Pop () method.

Example

In this example, we will create an array and delete the first element of the array. Note that this will also change the length of the array:

<script type= "Text/javascript" >var arr = new Array (3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" Document.writ E (arr + "<br/>") document.write ( arr.shift() + "<br/>") document.write (arr) </script>

Output:

George,john,thomasgeorgejohn,thomas
tiy
Shift ()
How to use Shift () to delete and return the first element of an array.

JavaScript Shift () 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.