Access Array Elements

3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€34.34
List Price:  €68.70
You save:  €34.35
£29.75
List Price:  £59.51
You save:  £29.76
CA$55.26
List Price:  CA$110.54
You save:  CA$55.28
A$61.34
List Price:  A$122.71
You save:  A$61.36
S$51.56
List Price:  S$103.13
You save:  S$51.57
HK$311.87
List Price:  HK$623.83
You save:  HK$311.95
CHF 32.19
List Price:  CHF 64.39
You save:  CHF 32.20
NOK kr403.80
List Price:  NOK kr807.72
You save:  NOK kr403.91
DKK kr256.36
List Price:  DKK kr512.80
You save:  DKK kr256.43
NZ$68.25
List Price:  NZ$136.51
You save:  NZ$68.26
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,863.12
List Price:  ৳9,727.46
You save:  ৳4,864.33
₹3,524.96
List Price:  ₹7,050.81
You save:  ₹3,525.84
RM169.05
List Price:  RM338.15
You save:  RM169.10
₦61,064.73
List Price:  ₦122,144.73
You save:  ₦61,080
₨11,267.18
List Price:  ₨22,537.18
You save:  ₨11,270
฿1,293.08
List Price:  ฿2,586.50
You save:  ฿1,293.41
₺1,646.44
List Price:  ₺3,293.30
You save:  ₺1,646.85
B$218.38
List Price:  B$436.81
You save:  B$218.43
R711.68
List Price:  R1,423.54
You save:  R711.85
Лв67.10
List Price:  Лв134.21
You save:  Лв67.11
₩55,741.82
List Price:  ₩111,497.59
You save:  ₩55,755.76
₪134.54
List Price:  ₪269.12
You save:  ₪134.57
₱2,284.41
List Price:  ₱4,569.39
You save:  ₱2,284.98
¥5,931.38
List Price:  ¥11,864.24
You save:  ¥5,932.86
MX$750.38
List Price:  MX$1,500.95
You save:  MX$750.57
QR145.58
List Price:  QR291.21
You save:  QR145.62
P536.90
List Price:  P1,073.94
You save:  P537.04
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,941.39
List Price:  E£3,883.26
You save:  E£1,941.87
ብር5,716.16
List Price:  ብር11,433.76
You save:  ብር5,717.59
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,726.71
List Price:  CLP$77,463.11
You save:  CLP$38,736.40
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,518.39
List Price:  RD$5,037.41
You save:  RD$2,519.02
DA5,195.45
List Price:  DA10,392.21
You save:  DA5,196.75
FJ$90.43
List Price:  FJ$180.89
You save:  FJ$90.46
Q306.32
List Price:  Q612.73
You save:  Q306.40
GY$8,355.08
List Price:  GY$16,712.25
You save:  GY$8,357.17
ISK kr4,932.76
List Price:  ISK kr9,866.76
You save:  ISK kr4,934
DH363.26
List Price:  DH726.63
You save:  DH363.36
L668.13
List Price:  L1,336.43
You save:  L668.29
ден2,110.62
List Price:  ден4,221.77
You save:  ден2,111.14
MOP$320.92
List Price:  MOP$641.92
You save:  MOP$321
N$704.48
List Price:  N$1,409.15
You save:  N$704.66
C$1,469.67
List Price:  C$2,939.71
You save:  C$1,470.03
रु5,632.07
List Price:  रु11,265.56
You save:  रु5,633.48
S/141.26
List Price:  S/282.56
You save:  S/141.30
K166.62
List Price:  K333.28
You save:  K166.66
SAR150.04
List Price:  SAR300.13
You save:  SAR150.08
ZK949.74
List Price:  ZK1,899.73
You save:  ZK949.98
L174.39
List Price:  L348.82
You save:  L174.43
Kč839.73
List Price:  Kč1,679.67
You save:  Kč839.94
Ft13,526.45
List Price:  Ft27,056.29
You save:  Ft13,529.83
SEK kr378.31
List Price:  SEK kr756.72
You save:  SEK kr378.41
ARS$54,428.90
List Price:  ARS$108,871.42
You save:  ARS$54,442.52
Bs275.96
List Price:  Bs551.99
You save:  Bs276.03
COP$160,141.69
List Price:  COP$320,323.44
You save:  COP$160,181.74
₡20,189.65
List Price:  ₡40,384.34
You save:  ₡20,194.69
L1,046.93
List Price:  L2,094.13
You save:  L1,047.20
₲288,443.91
List Price:  ₲576,959.95
You save:  ₲288,516.04
$U1,602.41
List Price:  $U3,205.22
You save:  $U1,602.81
zł146.11
List Price:  zł292.25
You save:  zł146.14
Already have an account? Log In

Transcript

The question now is why would we use arrays? And not just simple variables? The answer is that, in reality in real program, it's impossible just to create a program that consists of three or four elements, or three or four values, that's impossible. In reality, you are going to create programs that have many, many items, and variables. And it's impossible just to keep track of these, all of these items. So using arrays will make your life much much easier by combining similar elements, similar valuables, to only one variable, which is an array.

So now suppose that we have these three elements inside of just declaring these three elements as each one on a single line. I'm going to create an array again called foods equals two Item one, item one and item two, and item three, instead of just adding the string inside. And now we want to access a specific item of this array. How do we do we do so? Well the answer is so simple. In order to access a specific element inside an array, is that you use the index something called index.

Two arrays have an index have an index that starts with zero. So for example, the first element has an index of zero. The second element has an index of one, the third element has an index of two and so on and so forth. So the item that you want to access suppose that you want to access item number two, right? The index that you should add should be equal to That item minus one, which means we have, we should have an index of one. So keep that in mind.

Always remember that the index, the variable or the element that you want to access from an array equals to the number of that element minus one, which is going to give you the index of that number. So here I'm going to show you, for example, I'm going to use the console dot log. And I'm going to use the foods array. And I'm going to use the square brackets. And inside them suppose that they want to access element number one. So element number one has an index of one minus one which is zero.

So I'm going to add zero and save and run and I'm going to get apple. Now suppose that they want to access item number three. So here I'm going to type three more This one, which is to say I'm going to add two and if I save and run, I am going to get banana. So that is true. So this is how you can access elements from an array.

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.