Translation Push and Pop methods for JavaScript sequences

Source: Internet
Author: User
Tags array length

This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube

SOURCE Address here:

https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b

In this video we discuss the push () and Pop ( ) methods in JavaScript. We'll also talk about shift ( ) and unshift () . Methods. Let's use examples to help understand them.

In the following example, we use the For Loop and the series index to fill the sequence myarray. Then we use another for loop to get the elements in the sequence, and then we use JavaScript alert to show the length of the sequence.

var myArray = [];  for (var i = 0; I <= 5; i++) {    = i * 2;}  for (var i = 0; I <= 5; i++) {    + "<br/>");} alert (myarray.length);

Note: When you use the series index to get a sequence of elements, it does not change the length of the sequence.

Javascript push () method

This method adds a new element to the end of the sequence. This method will change the length of the sequence.

Javascript Pop () method

This method removes an element from the end of the sequence and then returns the element. This method changes the length of the sequence

Example: In the following example, we use push () to populate the sequence and use the Pop () method to get the element. Note that push () and pop () change the length property of the series

var myArray = [];  for (var i = 0; I <= 5; i++) {    * 2);} alert (myarray.length);  for (var i = 0; I <= 5; i++) {    + "<br/>");} alert (myarray.length);

Javascript Unshift () method

The push () method adds a new element to the end of the sequence. To add a new element to the front of the sequence, we use the Unshift () method. Similar to the push () method, the Unshift () method also changes the length of the sequence

Example:

var myArray = [2,3]; // Adds element 4 after element 3Myarray.push (4); // Adds element 1 before element 2myarray.unshift (1);d ocument.write ("Array elements =" + MyArray + "<b R/> ");d ocument.write (" Array Length = "+ myarray.length);

Javascript Shift () method

The Pop () method removes the last element in the sequence and returns the element. The shift () method removes the first element in the sequence and returns the element. Similar to the Pop () method, the Shift () method also changes the length of the sequence

Example:

var myArray = [1, 2, 3, 4, 5]; // removes the last element i.e 5 from the array var lastelement = myarray.pop ();d ocument.write ("last element =" + lastelement + "<br/>"); 
    //  removes the first element i.e 1 from the arrayvar firstelement = myarray.shift ();d OC Ument.write ("first element =" + firstelement + "<br/><br/>");d ocument.write ("Array elements = "+ MyArray +" <br/> ");d ocument.write (" Array Length = "+ myarray.length);

[Push and Pop methods for]javascript sequence

Related Article

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.