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
€61.92
List Price:  €88.46
You save:  €26.54
£52.75
List Price:  £75.37
You save:  £22.61
CA$96.72
List Price:  CA$138.19
You save:  CA$41.46
A$108.57
List Price:  A$155.11
You save:  A$46.54
S$90.97
List Price:  S$129.96
You save:  S$38.99
HK$542.42
List Price:  HK$774.92
You save:  HK$232.50
CHF 57.88
List Price:  CHF 82.69
You save:  CHF 24.81
NOK kr728.72
List Price:  NOK kr1,041.08
You save:  NOK kr312.35
DKK kr462.23
List Price:  DKK kr660.36
You save:  DKK kr198.13
NZ$117.59
List Price:  NZ$167.99
You save:  NZ$50.40
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,531.88
List Price:  ৳12,188.92
You save:  ৳3,657.04
₹5,923.28
List Price:  ₹8,462.20
You save:  ₹2,538.91
RM298.85
List Price:  RM426.95
You save:  RM128.10
₦112,243.66
List Price:  ₦160,354.96
You save:  ₦48,111.30
₨19,730.13
List Price:  ₨28,187.10
You save:  ₨8,456.97
฿2,316.66
List Price:  ฿3,309.66
You save:  ฿993
₺2,699.97
List Price:  ₺3,857.27
You save:  ₺1,157.29
B$396.12
List Price:  B$565.91
You save:  B$169.79
R1,287.39
List Price:  R1,839.21
You save:  R551.82
Лв121.09
List Price:  Лв172.99
You save:  Лв51.90
₩97,975.68
List Price:  ₩139,971.25
You save:  ₩41,995.57
₪251.07
List Price:  ₪358.69
You save:  ₪107.62
₱3,895.85
List Price:  ₱5,565.74
You save:  ₱1,669.89
¥10,141.55
List Price:  ¥14,488.55
You save:  ¥4,347
MX$1,370.43
List Price:  MX$1,957.85
You save:  MX$587.41
QR254.83
List Price:  QR364.06
You save:  QR109.23
P953.17
List Price:  P1,361.73
You save:  P408.56
KSh9,053.20
List Price:  KSh12,933.70
You save:  KSh3,880.50
E£3,553.03
List Price:  E£5,075.98
You save:  E£1,522.94
ብር9,421.13
List Price:  ብር13,459.34
You save:  ብር4,038.20
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$66,221.03
List Price:  CLP$94,605.53
You save:  CLP$28,384.50
CN¥510.57
List Price:  CN¥729.42
You save:  CN¥218.85
RD$4,131.65
List Price:  RD$5,902.61
You save:  RD$1,770.96
DA9,301.20
List Price:  DA13,288
You save:  DA3,986.80
FJ$157.89
List Price:  FJ$225.56
You save:  FJ$67.67
Q539.19
List Price:  Q770.30
You save:  Q231.11
GY$14,646.40
List Price:  GY$20,924.33
You save:  GY$6,277.92
ISK kr9,050.40
List Price:  ISK kr12,929.70
You save:  ISK kr3,879.30
DH648.26
List Price:  DH926.13
You save:  DH277.86
L1,196.82
List Price:  L1,709.82
You save:  L512.99
ден3,809.22
List Price:  ден5,441.97
You save:  ден1,632.75
MOP$560.81
List Price:  MOP$801.19
You save:  MOP$240.38
N$1,306.98
List Price:  N$1,867.19
You save:  N$560.21
C$2,583.28
List Price:  C$3,690.56
You save:  C$1,107.28
रु9,492.56
List Price:  रु13,561.38
You save:  रु4,068.82
S/257.39
List Price:  S/367.72
You save:  S/110.32
K284.03
List Price:  K405.78
You save:  K121.74
SAR262.47
List Price:  SAR374.98
You save:  SAR112.50
ZK1,947.17
List Price:  ZK2,781.79
You save:  ZK834.62
L308.34
List Price:  L440.50
You save:  L132.16
Kč1,543.04
List Price:  Kč2,204.44
You save:  Kč661.40
Ft25,060.61
List Price:  Ft35,802.41
You save:  Ft10,741.80
SEK kr675.73
List Price:  SEK kr965.37
You save:  SEK kr289.64
ARS$82,080.77
List Price:  ARS$117,263.27
You save:  ARS$35,182.50
Bs485.21
List Price:  Bs693.19
You save:  Bs207.97
COP$298,764.47
List Price:  COP$426,824.69
You save:  COP$128,060.21
₡35,459.16
List Price:  ₡50,658.12
You save:  ₡15,198.95
L1,821.77
List Price:  L2,602.65
You save:  L780.87
₲562,262.91
List Price:  ₲803,267.16
You save:  ₲241,004.24
$U2,936.85
List Price:  $U4,195.69
You save:  $U1,258.83
zł264.93
List Price:  zł378.49
You save:  zł113.55
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.