You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/03-symbol/article.md
+26-2Lines changed: 26 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,30 @@ alert(id1 == id2); // false
33
33
34
34
If you are familiar with Ruby or another language that also has some sort of "symbols" -- please don't be misguided. JavaScript symbols are different.
35
35
36
+
````warn header="Symbols don't auto-convert to a string"
37
+
Most values in JavaScript support implicit conversion to a string. For instance, we can `alert` almost any value, and it will work. Symbols are special. They don't auto-convert.
38
+
39
+
For instance, this `alert` will show an error:
40
+
41
+
```js run
42
+
let id = Symbol("id");
43
+
*!*
44
+
alert(id); // TypeError: Cannot convert a Symbol value to a string
45
+
*/!*
46
+
```
47
+
48
+
If we really want to show a symbol, we need to call `.toString()` on it, like here:
49
+
```js run
50
+
let id = Symbol("id");
51
+
*!*
52
+
alert(id.toString()); // Symbol(id), now it works
53
+
*/!*
54
+
```
55
+
56
+
That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another.
57
+
````
58
+
59
+
36
60
37
61
## "Hidden" properties
38
62
@@ -95,7 +119,7 @@ let user = {
95
119
```
96
120
That's because we need the value from the variable `id` as the key, not the string "id".
97
121
98
-
### Symbols skipped by for..in
122
+
### Symbols are skipped by for..in
99
123
100
124
Symbolic properties do not participate in `for..in` loop.
101
125
@@ -132,7 +156,7 @@ let clone = Object.assign({}, user);
132
156
alert( clone[id] ); // 123
133
157
```
134
158
135
-
There's no paradox here. That's by design. The idea is that when we clone an object or merge objects, we usually want symbolic properties (like `id`) to be copied as well.
159
+
There's no paradox here. That's by design. The idea is that when we clone an object or merge objects, we usually want *all* properties to be copied (including symbols like `id`).
136
160
137
161
````smart header="Property keys of other types are coerced to strings"
138
162
We can only use strings or symbols as keys in objects. Other types are coerced to strings.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/05-object-toprimitive/article.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -211,7 +211,7 @@ For instance:
211
211
```
212
212
213
213
```smart header="Historical notes"
214
-
For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but the object is ignored (like if the method didn't exist).
214
+
For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but that object is ignored (like if the method didn't exist).
215
215
216
216
In contrast, `Symbol.toPrimitive` *must* return an primitive, otherwise there will be an error.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -719,6 +719,6 @@ For the full list, see the [manual](mdn:js/Array).
719
719
720
720
From the first sight it may seem that there are so many methods, quite difficult to remember. But actually that's much easier than it seems.
721
721
722
-
Look through the cheatsheet just to be aware of them. Then do the tasks of this chapter to practice, so that you have experience with most methods.
722
+
Look through the cheatsheet just to be aware of them. Then solve the tasks of this chapter to practice, so that you have experience with array methods.
723
723
724
-
Afterwards whether you need to do something with an array, and you don't know how -- come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side.
724
+
Afterwards whenever you need to do something with an array, and you don't know how -- come here, look at the cheatsheet and find the right method. Examples will help you to write it correctly. Soon you'll automatically remember the methods, without specific efforts from your side.
0 commit comments