Skip to content

Commit 6c7c7d3

Browse files
committed
Added a dot notation example
1 parent 34b51dc commit 6c7c7d3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

1-js/04-object-basics/01-object/article.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ That's because the dot requires the key to be a valid variable identifier. That
105105

106106
There's an alternative "square bracket notation" that works with any string:
107107

108-
109108
```js run
110109
let user = {};
111110

@@ -130,7 +129,7 @@ let key = "likes birds";
130129
user[key] = true;
131130
```
132131

133-
Here, the variable `key` may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility. The dot notation cannot be used in a similar way.
132+
Here, the variable `key` may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility.
134133

135134
For instance:
136135

@@ -146,6 +145,17 @@ let key = prompt("What do you want to know about the user?", "name");
146145
alert( user[key] ); // John (if enter "name")
147146
```
148147

148+
The dot notation cannot be used in a similar way.
149+
150+
```js run
151+
let user = {
152+
name: "John",
153+
age: 30
154+
};
155+
156+
let key = "name";
157+
user.key // undefined
158+
```
149159

150160
### Computed properties
151161

0 commit comments

Comments
 (0)