JavaScript array push and pop methods

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss push() and pop() methods in JavaScript. Along the way we will also discuss shift() and unshift() methods. Let us understand these methods with examples.

In the example below, we are populating myArray using a for loop and the array index. Subsequently we are using another for loop to retrieve the elements from the array. Finally we are displaying the length of the array using JavaScript alert.

var myArray = [];

for (var i = 0; i [= 5; i++)
{
myArray[i] = i * 2;
}

for (var i = 0; i [= 5; i++)
{
}

Please note : Retrieving array elements using the array index, will not change the length of the array.

JavaScript push() method
This method adds new items to the end of the array. This method also changes the length of the array.

JavaScript pop() method
This method removes the last element of an array, and returns that element. This method changes the length of an array.

Example : In the exampe below, we are using push() method to populate the array and pop() method to retrieve elements from the array. Notice that push() and pop() methods change the length property of the array.

var myArray = [];

for (var i = 0; i [= 5; i++)
{
}

for (var i = 0; i [= 5; i++)
{
}

JavaScript unshift() Method
push() method adds new items to the end of the array. To add new items to the beginning of an array, then use unshift() method. Just like push() method, unshift() method also changes the length of an array

Example :
var myArray = [2,3];

// Adds element 4 after element 3

// Adds element 1 before element 2

JavaScript shift() Method
pop() method removes the last element of an array, and returns that element. shift() method removes the first item of an array, and returns that item. Just like pop() method, shift() method also changes the length of an array.

Example :

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

// removes the last element i.e 5 from the array

// removes the first element i.e 1 from the array

Рекомендации по теме
Комментарии
Автор

Very comprehensive explanation. Thanks for taking the time to actually show what is happening by showing the actual values. So much more helpful than just a verbal explanation of what is going on.

squeakal
Автор

Thank you very much for doing this. At first place, your videos helped me to nail 70-483, now I am working on 70-480. God bless!

denisfrolov
Автор

Thanks Venkat Sir, You are the LEGEND!

vishnukanth
Автор

the way you arrange video very easy to learn, it is very order from basic to advanced.

nguyenquan
Автор

Please update the title to consider shift and unshift functions.

rupenanjaria
Автор

Thank you. This was great. Do you have any videos on the flow of data? Without using .shift and .unshift, how can you get rid of all negative numbers in an array and only returning positive numbers in the original array?

phantomsmokes
Автор

May I know this javascript videos series enough to cover all JavaScript oops and MVC Methods ?

jitujahagirdar
Автор

why do we have to include the html break tag..the escape sequence for new line "\n" doesnt work..why is that so

ankitkothari
Автор

These video s enough for 2 experience
too? please let me know

arjunv
join shbcf.ru