Common methods of JS array operation

Source: Internet
Author: User

Push (): Adds an element after the array and returns the length of the array;

Unshift (): In front of the array is like an element, and returns the length of the array;

Pop (): Deletes the last element;

var arr =[1,2,3,4,5];

Arr.push (6,7,8);

Console.log (arr);//push: The result of adding an element after the array is: [1,2,3,4,5,6,7,8]

Arr AA = [7,8,9,0];

Aa.unshift (4,5,6);

Console.log (AA);//unshift: The result of adding an element before the array is: [4,5,6,7,8,9,0]

var arr = [1,2,3,4,5];

Console.log (Arr.push (6)); The direct console, which returns the length of the array, is 6

var AA = [9,0];

Console.log (Aa.unshift (5)); The direct console, which returns the length of the array, is 3

Pop () Delete last element

var arrdele =[1,2,3];
Console.log (Arrdele.pop ());//returns the last element and deletes
Console.log (Arrdele);
Arrdele.pop ();//Delete last element
Console.log (Arrdele);//Returns an array

Shift () deletes the first element

var Delearr =[1,2,3,4];
Console.log (Delearr.shift ());//returns the first element and deletes
Console.log (Delearr);
Delearr.shift ();//Delete First element
Console.log (Delearr);

Common methods of JS array operation

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.