JavaScript array functions: unshift, shift, pop, and push use of instance _ javascript skills

Source: Internet
Author: User
Tags javascript array
This article mainly introduces the use cases of JavaScript Array functions unshift, shift, pop, and push. This article first explains how to declare arrays, and then provides some examples for the use of the four functions, for more information, see How to declare an array

There are several methods to declare arrays in s.

The Code is as follows:


Var tmp = []; // shorthand Mode
Var tmp = new Array (); // a new
Var tmp = Array (); // or new


You can input a parameter in the new array to indicate the initialization length of the array.

The Code is as follows:


// Input a parameter when the value is new to indicate the length of the initialized array.
Var tmp = new Array (3 );

Alert (tmp. length); // 3


However, if you want to create an array with only one element 3, the new method cannot be used, because the system regards the input 3 as the length of the array, unless you use quotation marks as a string, such

The Code is as follows:


Var tmp = new Array ('3 ');
Alert (tmp); // 3


You can create an array with only one number element 3 in the abbreviated mode.

The Code is as follows:


Var tmp = [3]
Alert (typeof tmp [0]); // number


You can also initialize multiple elements, and the element values can be of any type.

The Code is as follows:


// Create an array in simple mode
// The element of the array can be any data type
Var tmp = [3, true, 8.5, {'name': 'lizhong '}, ['A',' B '];
Alert (tmp. length); // 5

1. unshift inserts an element before the first element of the array

The Code is as follows:


// Use unshift to insert an element before the first element of the array
// Returns the length of the array.
Var tmp = ['A', 'B'];
Var len = tmp. unshift ('C ');
Alert (len); // 3
Alert (tmp); // c, a, B


You can also insert multiple elements at a time, starting from the left

The Code is as follows:


// Use unshift to insert an element before the first element of the array
// Returns the length of the array.
Var tmp = ['A', 'B'];
Var len = tmp. unshift ('C', 'D ');
Alert (len); // 4
Alert (tmp); // c, d, a, B

2. shift pops up the first element of the array and returns the element value to be popped up.

Small instance:

The Code is as follows:


// Use shift to pop up the first element of the array
// Return the pop-up element value
Var tmp = ['A', 'B', 'C'];
Var val = tmp. shift ();
Alert (val); //
Alert (tmp); // B, c


If it is an empty array:

The Code is as follows:


// Use shift to pop up the first element of the array
// Return the pop-up element value
Var tmp = [];
Var val = tmp. shift ();
Alert (val); // undefined
Alert (tmp); // null

3. add elements to the end of the array by push

In contrast to unshift, push adds an element to the end of the array and returns the length of the array after the element is added.

The Code is as follows:


// Use push to add multiple elements at the end of the array
// Returns the latest length of the array.
Var tmp = ['A', 'B', 'C'];
Var len = tmp. push ('D ');
Alert (len); // 4
Alert (tmp); // a, B, c, d


You can also add multiple elements at a time.

The Code is as follows:


// Use push to add multiple elements at the end of the array
// Returns the latest length of the array.
Var tmp = ['A', 'B', 'C'];
Var len = tmp. push ('D', 'E', 'F ');
Alert (len); // 6
Alert (tmp); // a, B, c, d, e, f

IV. The pop function deletes elements at the end of an array.

In contrast to shift, pop pops up the elements at the end of the array, and returns the element value to be popped up.

The Code is as follows:


// Use pop to pop up the elements at the end of the array
// Return the pop-up element value
Var tmp = ['A', 'B', 'C'];
Var val = tmp. pop ();
Alert (val); // c
Alert (tmp); // a, B


If the array is empty, undefined is returned.

The Code is as follows:


// Use pop to pop up the elements at the end of the array
// Return the pop-up element value
Var tmp = [];
Var val = tmp. pop ();
Alert (val); // undefined
Alert (tmp); // null



With the above four functions, we can do some queue processing, and the specific case will not write code.
The push function can also be implemented in this way.

The Code is as follows:


Var tmp = ['A', 'B', 'C'];
Tmp [tmp. length] = 'D ';
Alert (tmp); // a, B, c, d


Note: The unshift, shift, pop, and push operations of the above four functions are all modified on the array itself.

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.