diff --git a/1-js/04-object-basics/03-symbol/article.md b/1-js/04-object-basics/03-symbol/article.md index 4509b60b..b038761f 100644 --- a/1-js/04-object-basics/03-symbol/article.md +++ b/1-js/04-object-basics/03-symbol/article.md @@ -33,6 +33,30 @@ alert(id1 == id2); // false 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. +````warn header="Symbols don't auto-convert to a string" +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. + +For instance, this `alert` will show an error: + +```js run +let id = Symbol("id"); +*!* +alert(id); // TypeError: Cannot convert a Symbol value to a string +*/!* +``` + +If we really want to show a symbol, we need to call `.toString()` on it, like here: +```js run +let id = Symbol("id"); +*!* +alert(id.toString()); // Symbol(id), now it works +*/!* +``` + +That's a "language guard" against messing up, because strings and symbols are fundamentally different and should not occasionally convert one into another. +```` + + ## "Hidden" properties @@ -95,7 +119,7 @@ let user = { ``` That's because we need the value from the variable `id` as the key, not the string "id". -### Symbols skipped by for..in +### Symbols are skipped by for..in Symbolic properties do not participate in `for..in` loop. @@ -132,7 +156,7 @@ let clone = Object.assign({}, user); alert( clone[id] ); // 123 ``` -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. +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`). ````smart header="Property keys of other types are coerced to strings" We can only use strings or symbols as keys in objects. Other types are coerced to strings. diff --git a/1-js/04-object-basics/05-object-toprimitive/article.md b/1-js/04-object-basics/05-object-toprimitive/article.md index d39fe523..bd2fc771 100644 --- a/1-js/04-object-basics/05-object-toprimitive/article.md +++ b/1-js/04-object-basics/05-object-toprimitive/article.md @@ -211,7 +211,7 @@ For instance: ``` ```smart header="Historical notes" -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). +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). In contrast, `Symbol.toPrimitive` *must* return an primitive, otherwise there will be an error. ``` diff --git a/1-js/05-data-types/05-array-methods/article.md b/1-js/05-data-types/05-array-methods/article.md index a0023a20..11905ba4 100644 --- a/1-js/05-data-types/05-array-methods/article.md +++ b/1-js/05-data-types/05-array-methods/article.md @@ -719,6 +719,6 @@ For the full list, see the [manual](mdn:js/Array). 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. -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. +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. -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. +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.