Accessing Properties with [ ]

Modern JavaScript Using Objects
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.03
List Price:  €134.44
You save:  €38.41
£78.97
List Price:  £110.57
You save:  £31.59
CA$143.92
List Price:  CA$201.50
You save:  CA$57.57
A$160.15
List Price:  A$224.22
You save:  A$64.06
S$135.90
List Price:  S$190.26
You save:  S$54.36
HK$776.95
List Price:  HK$1,087.76
You save:  HK$310.81
CHF 89.61
List Price:  CHF 125.46
You save:  CHF 35.85
NOK kr1,132.24
List Price:  NOK kr1,585.18
You save:  NOK kr452.94
DKK kr716.48
List Price:  DKK kr1,003.10
You save:  DKK kr286.62
NZ$176.72
List Price:  NZ$247.41
You save:  NZ$70.69
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,949.45
List Price:  ৳16,729.71
You save:  ৳4,780.25
₹8,504.71
List Price:  ₹11,906.93
You save:  ₹3,402.22
RM450.50
List Price:  RM630.72
You save:  RM180.22
₦155,597.43
List Price:  ₦217,842.63
You save:  ₦62,245.20
₨27,817.89
List Price:  ₨38,946.16
You save:  ₨11,128.27
฿3,454.30
List Price:  ฿4,836.16
You save:  ฿1,381.86
₺3,507.24
List Price:  ₺4,910.28
You save:  ₺1,403.03
B$630.94
List Price:  B$883.35
You save:  B$252.40
R1,822.81
List Price:  R2,552.01
You save:  R729.20
Лв187.70
List Price:  Лв262.78
You save:  Лв75.08
₩144,917.66
List Price:  ₩202,890.52
You save:  ₩57,972.86
₪361.94
List Price:  ₪506.74
You save:  ₪144.79
₱5,900.06
List Price:  ₱8,260.32
You save:  ₱2,360.26
¥15,702.92
List Price:  ¥21,984.72
You save:  ¥6,281.80
MX$2,038.30
List Price:  MX$2,853.70
You save:  MX$815.40
QR368.69
List Price:  QR516.19
You save:  QR147.49
P1,365.11
List Price:  P1,911.21
You save:  P546.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,089.47
List Price:  E£7,125.47
You save:  E£2,035.99
ብር12,635.69
List Price:  ብር17,690.47
You save:  ብር5,054.78
Kz91,513.08
List Price:  Kz128,121.98
You save:  Kz36,608.89
CLP$99,000
List Price:  CLP$138,603.96
You save:  CLP$39,603.96
CN¥729.57
List Price:  CN¥1,021.43
You save:  CN¥291.86
RD$6,083.32
List Price:  RD$8,516.89
You save:  RD$2,433.57
DA13,417.46
List Price:  DA18,784.98
You save:  DA5,367.52
FJ$231.78
List Price:  FJ$324.51
You save:  FJ$92.72
Q770.27
List Price:  Q1,078.41
You save:  Q308.14
GY$20,904.88
List Price:  GY$29,267.67
You save:  GY$8,362.78
ISK kr13,874.61
List Price:  ISK kr19,425.01
You save:  ISK kr5,550.40
DH998.53
List Price:  DH1,397.99
You save:  DH399.45
L1,842.29
List Price:  L2,579.28
You save:  L736.99
ден5,908.76
List Price:  ден8,272.50
You save:  ден2,363.74
MOP$800.37
List Price:  MOP$1,120.56
You save:  MOP$320.18
N$1,804.90
List Price:  N$2,526.94
You save:  N$722.03
C$3,679.90
List Price:  C$5,152.01
You save:  C$1,472.10
रु13,596.02
List Price:  रु19,034.98
You save:  रु5,438.95
S/373.62
List Price:  S/523.08
You save:  S/149.46
K405.04
List Price:  K567.08
You save:  K162.03
SAR375.67
List Price:  SAR525.96
You save:  SAR150.28
ZK2,767.31
List Price:  ZK3,874.34
You save:  ZK1,107.03
L477.70
List Price:  L668.80
You save:  L191.10
Kč2,412.77
List Price:  Kč3,377.98
You save:  Kč965.20
Ft39,808.32
List Price:  Ft55,733.24
You save:  Ft15,924.92
SEK kr1,103.71
List Price:  SEK kr1,545.23
You save:  SEK kr441.52
ARS$102,213.30
List Price:  ARS$143,102.71
You save:  ARS$40,889.41
Bs690.92
List Price:  Bs967.31
You save:  Bs276.39
COP$438,077.06
List Price:  COP$613,325.41
You save:  COP$175,248.35
₡50,326.04
List Price:  ₡70,458.47
You save:  ₡20,132.42
L2,538.30
List Price:  L3,553.73
You save:  L1,015.42
₲780,569.17
List Price:  ₲1,092,828.06
You save:  ₲312,258.89
$U4,458.42
List Price:  $U6,241.97
You save:  $U1,783.54
zł409.45
List Price:  zł573.24
You save:  zł163.79
Already have an account? Log In

Transcript

So far, we have used the dot syntax to access properties within an object. Well, there's another way to do it, you can do it with the square brackets as well. And this is an important concept to understand, because of the dynamic nature of using square brackets to access the properties. It allows for more flexibility, and can be very useful in solving certain programming problems. So let's take a look at this. Let me open the console.

And I'm going to create an object again. Similar to the objects we've been creating in our other exercises has a first name last name has a function, age birthdate. Well, some of the things that are part of this object, go ahead and press return to define that object. Now, as we've done in the past, if I want to access the first name, Use the dot syntax. And that returns the first name, I can assign that to a variable. I can log it out, I can do whatever I need to with that.

Now, the other way of accessing these properties is using the square brackets. So I could do the exact same thing. Using square brackets and inside of the square brackets, I put a string literal, that evaluates to the name in the name value pair or the key value pair. So in this case, I would put first name and quotes that will resolve to first name and it will be like entering you user dot first name. Go ahead and press return. And sure enough, we get the same thing back.

Now why is this useful? Why do we know why do we need to know the second method of accessing properties. Well, what this allows is that now, you can use a variable to access a property. Let's say you're writing some code and you're not sure, because of the nature of the code, you're not sure what property is going to need to be extracted from an object. The result is based upon what some code determines, well, you can use a variable and then that variable can be used with the square brackets to access that property. So for example, let's say I have a variable, name part.

And I'm gonna set that equal to first name. Now, let's say that that data came from a web page. Somebody entered first name in a form to indicate what name they want. To retrieve. And so, me writing the code in the background, I don't know what what name, they're going to enter there. And so I just set the value into a variable.

And then I use the variable to extract that from an object like this square brackets. Then inside the square brackets, I simply put the variable because that variable will evaluate to something. And that something is what will be retrieved from the object press return. And sure enough, we get Sharon. So you can see that adds a lot more flexibility to the accessing of properties within an object. All right, let me show you one more example.

That illustrates the power of this. So I'm going to jump out to sublime. Over here in this JavaScript page, I have the following First, I have a pretty simple object. It's Now, it's assigned to a customer variable. And it simply has three properties, which are each part of an address, the name, the street, the city, state, and zip. Now down here, I want to create that address so I can print it out.

I want to put the whole address together. And so I declare a variable, set it equal to an empty string and then I go through a for loop. I is equal to zero, i less than three, so it'll go until I is two and then I increment i and then I start building the address addr plus equal to so I use that assignment operator, where I add to what is already in addr. This information, which is using the square brackets, and it accesses the address the properties based upon what I is, and so I do address and concatenate I to it, and then that will extract from the object, the portion of the address I'm after. Then here at the end I concatenate. This is called an escape sequence.

And this is an escape character specifically, and I concatenate that to the end. And basically what this escape sequence does is it puts a carriage return so backslash and puts a carriage return. And so it will put the whole address together with a carriage return between each part. And then I simply log it to the console so I can see what it looks like. So let's go ahead and take a look at that. copy the file path, paste that in and open the console.

And we have a nice formatted address and so that for loop able to go through that object using the brackets and access each of the properties within the object. So very powerful concept for accessing properties. Let's move on to the next section.

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.