Removing the last element from the end of a JavaScript array

JavaScript for Beginners - Arrays: Adding and Removing Elements Part I - Working with the ends of a JavaScript array
2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.87
List Price:  €96.96
You save:  €29.09
£56.33
List Price:  £80.48
You save:  £24.14
CA$101.11
List Price:  CA$144.46
You save:  CA$43.34
A$112.90
List Price:  A$161.30
You save:  A$48.39
S$95.89
List Price:  S$136.99
You save:  S$41.10
HK$544.41
List Price:  HK$777.77
You save:  HK$233.35
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr795.45
List Price:  NOK kr1,136.41
You save:  NOK kr340.95
DKK kr506.37
List Price:  DKK kr723.41
You save:  DKK kr217.04
NZ$125.48
List Price:  NZ$179.27
You save:  NZ$53.78
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,489.45
List Price:  ৳12,128.31
You save:  ৳3,638.85
₹6,003.32
List Price:  ₹8,576.54
You save:  ₹2,573.22
RM314.95
List Price:  RM449.95
You save:  RM135
₦108,169.08
List Price:  ₦154,533.88
You save:  ₦46,364.80
₨19,460.08
List Price:  ₨27,801.31
You save:  ₨8,341.22
฿2,414.58
List Price:  ฿3,449.55
You save:  ฿1,034.97
₺2,476.26
List Price:  ₺3,537.66
You save:  ₺1,061.40
B$432.69
List Price:  B$618.15
You save:  B$185.46
R1,309.65
List Price:  R1,871.01
You save:  R561.36
Лв132.94
List Price:  Лв189.93
You save:  Лв56.98
₩102,666.23
List Price:  ₩146,672.33
You save:  ₩44,006.10
₪255.59
List Price:  ₪365.15
You save:  ₪109.55
₱4,073.94
List Price:  ₱5,820.16
You save:  ₱1,746.22
¥11,002.07
List Price:  ¥15,717.92
You save:  ¥4,715.85
MX$1,443.99
List Price:  MX$2,062.93
You save:  MX$618.94
QR254.73
List Price:  QR363.91
You save:  QR109.18
P971.15
List Price:  P1,387.42
You save:  P416.26
KSh9,031.62
List Price:  KSh12,902.87
You save:  KSh3,871.24
E£3,551.78
List Price:  E£5,074.19
You save:  E£1,522.41
ብር8,922.62
List Price:  ብር12,747.15
You save:  ብር3,824.52
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$70,211.88
List Price:  CLP$100,306.99
You save:  CLP$30,095.10
CN¥512.36
List Price:  CN¥731.98
You save:  CN¥219.61
RD$4,268.13
List Price:  RD$6,097.60
You save:  RD$1,829.46
DA9,503.22
List Price:  DA13,576.62
You save:  DA4,073.39
FJ$162.81
List Price:  FJ$232.59
You save:  FJ$69.78
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,612.09
List Price:  GY$20,875.31
You save:  GY$6,263.22
ISK kr9,750.89
List Price:  ISK kr13,930.45
You save:  ISK kr4,179.55
DH704.78
List Price:  DH1,006.87
You save:  DH302.09
L1,300.73
List Price:  L1,858.26
You save:  L557.53
ден4,174.94
List Price:  ден5,964.46
You save:  ден1,789.51
MOP$559.86
List Price:  MOP$799.84
You save:  MOP$239.97
N$1,311.40
List Price:  N$1,873.51
You save:  N$562.11
C$2,571.06
List Price:  C$3,673.10
You save:  C$1,102.04
रु9,593.53
List Price:  रु13,705.64
You save:  रु4,112.10
S/262.27
List Price:  S/374.69
You save:  S/112.42
K279.90
List Price:  K399.88
You save:  K119.97
SAR262.85
List Price:  SAR375.52
You save:  SAR112.66
ZK1,946.04
List Price:  ZK2,780.17
You save:  ZK834.13
L337.70
List Price:  L482.45
You save:  L144.75
Kč1,708.24
List Price:  Kč2,440.45
You save:  Kč732.21
Ft28,222.76
List Price:  Ft40,319.96
You save:  Ft12,097.20
SEK kr777.78
List Price:  SEK kr1,111.16
You save:  SEK kr333.38
ARS$72,264.67
List Price:  ARS$103,239.67
You save:  ARS$30,975
Bs482.86
List Price:  Bs689.83
You save:  Bs206.97
COP$305,718.10
List Price:  COP$436,758.87
You save:  COP$131,040.76
₡35,617.57
List Price:  ₡50,884.43
You save:  ₡15,266.85
L1,775.85
List Price:  L2,537.04
You save:  L761.19
₲546,995.01
List Price:  ₲781,454.93
You save:  ₲234,459.92
$U3,074.18
List Price:  $U4,391.87
You save:  $U1,317.69
zł289.95
List Price:  zł414.23
You save:  zł124.28
Already have an account? Log In

Transcript

Removing the last element in the end of a JavaScript array. The JavaScript pop method provides a very simple way to remove the last element of the array. So once again, looking at our foo array, we have the elements A, B, and C. So what if I wanted to remove c? Well, that's the last element. So if I just want to remove the last element of the array, I can use the pop method to remove C, which is the last element. So when I do that, if I say food up, pop, C is removed from the array, but then I had the literacy here in the middle of the screen.

So what is this? Why is the literacy just sitting there? And that's because the pop method always returns the element that was removed from the array. So when I said food pop, it removed the last element which was C, but it returned C. So if I would execute that the pop method again, what would happen is, I would remove B from the end of the array, but it would actually the method would actually return the letter. Be. So let's look at a code example for that one.

In JavaScript, the pop method removes the last element of the array and returns that element. So here I've got an array called foo. And if I call the pop method, it should remove the last element, which is a letter F. And it will return that element that it removes the letter F, and the length will change from six to five. Let's see this in action. So first, I want to define the array. So I've got this array called foo now and if I inspect, foo, we'll see it's got an element with six.

It's an array with six elements. The last one is F. So now, if I call the pop method of foo, it should do two things, it should return the letter F, which we'll see in a second and the length of foo should change from six to five. So here it returned the letter F. And then if I do foo dot length, see the length of the array is now five. If I inspect food, You can see it no longer has a letter F. It's just ABCDE. So now if I call food pop again, it should return the letter E. And it should change the length from five to four. So execute pop, it returns the letter E. And then if I want to look at foo again, I can see that it's got four elements in it right.

And then if I execute, pop one more time, it should return the letter D, which is the last element of the array, and then foo should now have three elements in it. So the pop method always returns the last element of the array. It removes the last element and returns it. And of course, the length of that array will change

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.