MCITMC - Tin Tức Việt Nam chính xác, cập nhật trực tiếp 24/7
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh
No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh
No Result
View All Result
MCITMC - Tin Tức Việt Nam chính xác, cập nhật trực tiếp 24/7
No Result
View All Result

JavaScript array push and pop methods

admin by admin
March 29, 2020
in Công Nghệ
0
JavaScript array push and pop methods



Link for all dot net and sql server video tutorial playlists

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

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++)
{
document.write(myArray[i] + “[br/]”);
}

alert(myArray.length);

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++)
{
myArray.push(i * 2);
}

alert(myArray.length);

for (var i = 0; i [= 5; i++)
{
document.write(myArray.pop() + “[br/]”);
}

alert(myArray.length);

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
myArray.push(4);

// Adds element 1 before element 2
myArray.unshift(1);

document.write(“Array elements = ” + myArray + “[br/]”);
document.write(“Array Length = ” + myArray.length);

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
var lastElement = myArray.pop();
document.write(“Last element = ” + lastElement + “[br/]”);

// removes the first element i.e 1 from the array
var firstElement = myArray.shift();
document.write(“First element = ” + firstElement + “[br/][br/]”);

document.write(“Array elements = ” + myArray + “[br/]”);
document.write(“Array Length = ” + myArray.length);

Nguồn: https://mcitmc.org

Xem thêm bài viết khác: https://mcitmc.org/cong-nghe/

Xem thêm Bài Viết:

  • Chia sẻ thủ thuật download ArcGIS 10.2 Full Crack cực đơn giản
  • Bật mí chi tiết cách bẻ khóa wifi cho điện thoại Android
  • Bật mí cách tải iFile và cách cài đặt cho iPhone chi tiết nhất
  • Hướng dẫn cách đăng ký Appvn trên Android cực đơn giản, dễ dàng
  • Bật mí thủ thuật mod màn hình khóa Android cực đẹp, cực ngầu
Tags: Youtube
Previous Post

Trợ lý phòng mổ bị chụp ảnh khỏa thân trên bàn phẫu thuật

Next Post

Bán đất thổ cư thổ vườn Củ Chi 1.100m2 Phú Hòa Đông Tân Quy giá 5 tỷ

Next Post
Bán đất thổ cư thổ vườn Củ Chi 1.100m2 Phú Hòa Đông Tân Quy giá 5 tỷ

Bán đất thổ cư thổ vườn Củ Chi 1.100m2 Phú Hòa Đông Tân Quy giá 5 tỷ

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Xem Thêm

Công trình này của huyện Đông Anh đã cơ bản hoàn thiện.

Toàn cảnh 4 dự án hạ tầng trăm tỷ đồng đang thành hình ở huyện Đông Anh

June 29, 2022
CĂN HỘ WEST GATE

CĂN HỘ WEST GATE

June 29, 2022

Dự án chung cư Sun Square

June 20, 2022

Đánh giá quận Nam Từ Liêm

June 15, 2022
Dự án chung cư sắp mở bán tại TPHCM mới nhất 2022

Dự án chung cư sắp mở bán tại TPHCM mới nhất 2022

March 5, 2022
Chủ đầu tư Opal City View phát triển dự án theo hướng nào?

Chủ đầu tư Opal City View phát triển dự án theo hướng nào?

July 9, 2021
  • Liên Hệ
  • Chính Sách Bảo Mật

© 2022 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Kinh Doanh

© 2022 JNews - Premium WordPress news & magazine theme by Jegtheme.