Removing the first element 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
€62.18
List Price:  €88.83
You save:  €26.65
£52.34
List Price:  £74.77
You save:  £22.43
CA$97.54
List Price:  CA$139.35
You save:  CA$41.80
A$109.05
List Price:  A$155.79
You save:  A$46.74
S$90.66
List Price:  S$129.53
You save:  S$38.86
HK$547.93
List Price:  HK$782.79
You save:  HK$234.86
CHF 58.30
List Price:  CHF 83.29
You save:  CHF 24.99
NOK kr719
List Price:  NOK kr1,027.19
You save:  NOK kr308.19
DKK kr463.85
List Price:  DKK kr662.68
You save:  DKK kr198.82
NZ$118.29
List Price:  NZ$168.99
You save:  NZ$50.70
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,504.53
List Price:  ৳12,149.85
You save:  ৳3,645.31
₹5,993.29
List Price:  ₹8,562.22
You save:  ₹2,568.92
RM300.60
List Price:  RM429.45
You save:  RM128.85
₦112,540.42
List Price:  ₦160,778.92
You save:  ₦48,238.50
₨19,777.94
List Price:  ₨28,255.42
You save:  ₨8,477.47
฿2,314.46
List Price:  ฿3,306.51
You save:  ฿992.05
₺2,719.65
List Price:  ₺3,885.38
You save:  ₺1,165.73
B$395.07
List Price:  B$564.42
You save:  B$169.34
R1,261.94
List Price:  R1,802.84
You save:  R540.90
Лв121.52
List Price:  Лв173.61
You save:  Лв52.08
₩97,500.91
List Price:  ₩139,292.99
You save:  ₩41,792.07
₪246.92
List Price:  ₪352.77
You save:  ₪105.84
₱3,899.17
List Price:  ₱5,570.49
You save:  ₱1,671.31
¥10,112.57
List Price:  ¥14,447.15
You save:  ¥4,334.58
MX$1,348.11
List Price:  MX$1,925.96
You save:  MX$577.84
QR255.13
List Price:  QR364.49
You save:  QR109.35
P947.18
List Price:  P1,353.18
You save:  P405.99
KSh9,035.70
List Price:  KSh12,908.70
You save:  KSh3,873
E£3,494.55
List Price:  E£4,992.44
You save:  E£1,497.88
ብር9,426.74
List Price:  ብር13,467.35
You save:  ብር4,040.61
Kz63,827.73
List Price:  Kz91,186.39
You save:  Kz27,358.65
CLP$65,866.18
List Price:  CLP$94,098.58
You save:  CLP$28,232.40
CN¥505.29
List Price:  CN¥721.87
You save:  CN¥216.58
RD$4,124.72
List Price:  RD$5,892.71
You save:  RD$1,767.99
DA9,302.64
List Price:  DA13,290.06
You save:  DA3,987.41
FJ$158.71
List Price:  FJ$226.74
You save:  FJ$68.02
Q539.73
List Price:  Q771.08
You save:  Q231.34
GY$14,644.90
List Price:  GY$20,922.19
You save:  GY$6,277.28
ISK kr9,072.80
List Price:  ISK kr12,961.70
You save:  ISK kr3,888.90
DH646.48
List Price:  DH923.59
You save:  DH277.10
L1,217.85
List Price:  L1,739.87
You save:  L522.01
ден3,827.32
List Price:  ден5,467.84
You save:  ден1,640.51
MOP$563.88
List Price:  MOP$805.58
You save:  MOP$241.70
N$1,269.34
List Price:  N$1,813.43
You save:  N$544.08
C$2,586.61
List Price:  C$3,695.31
You save:  C$1,108.70
रु9,563.33
List Price:  रु13,662.48
You save:  रु4,099.15
S/258.06
List Price:  S/368.68
You save:  S/110.61
K292.18
List Price:  K417.41
You save:  K125.23
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,888.41
List Price:  ZK2,697.85
You save:  ZK809.43
L316.29
List Price:  L451.87
You save:  L135.57
Kč1,549.45
List Price:  Kč2,213.59
You save:  Kč664.14
Ft25,035.70
List Price:  Ft35,766.82
You save:  Ft10,731.12
SEK kr676.41
List Price:  SEK kr966.35
You save:  SEK kr289.93
ARS$79,681.84
List Price:  ARS$113,836.08
You save:  ARS$34,154.24
Bs484.72
List Price:  Bs692.49
You save:  Bs207.76
COP$292,933.30
List Price:  COP$418,494.08
You save:  COP$125,560.78
₡35,419.50
List Price:  ₡50,601.46
You save:  ₡15,181.95
L1,821.37
List Price:  L2,602.07
You save:  L780.69
₲561,481.10
List Price:  ₲802,150.24
You save:  ₲240,669.13
$U2,931.93
List Price:  $U4,188.65
You save:  $U1,256.72
zł264
List Price:  zł377.16
You save:  zł113.16
Already have an account? Log In

Transcript

Removing the first element of a JavaScript array. Well, the shift method provides a very simple way of removing the first element of a JavaScript array. So back to our foo array. This time the array contains the letters A, B, and C. So what if I wanted to remove a from the rear of the array, I want to remove the first element of the array? Well, I could call the shift method. And when I call the shift method, what happens is a is removed from the array.

But then I wind up with a letter a kind of floating around there. And my next question is, well, what is this? Why don't I get the letter A there. And that's because the shift method always returns the element that was removed from the array. So if I were to call the shift method one more time here, it would remove the letter B, and return it because the letter B was the first element in the array. It was the first element and now I call shift again, and it removes B from the array, but it returns the letter B, it returns the element that was removed from the array.

So let's look at one more code example for this one. in JavaScript, the shift method removes the first element of an array and returns that element. Here I have an array with six elements, the first element is the letter A. So if I call the shift method on that array, it should remove the first element, which is a letter A and return it. Let's give it a try. So first, I'll define this array and I now have an array called foo, that has six elements and the first element is the letter A.

So now if I call the shift method on this array, it should remove the first element which is the letter A, and it should return that element. So return the letter A. And if I examine the array, we can see that it did remove the first element which is the letter A. So if I call the shift method again, it should remove returns a letter B. And I can see that if I examine foo, the letter B is missing and the letter C is now the first element is only for L. In the array, so if I call shift one more time, it removed the first element, which is a letter C. And if I examine foo, I can see it only has three elements. Because the first element of C was removed, the first element is D. So in JavaScript, the shift method, always returns removes the first element of an array and it returns the element that it removed.

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.