Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update Map usage explanation in article.md #3903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ayush007-git wants to merge 2 commits into javascript-tutorial:master
base: master
Choose a base branch
Loading
from ayush007-git:patch-1
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions 1-js/05-data-types/07-map-set/article.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ alert( map.size ); // 3

As we can see, unlike objects, keys are not converted to strings. Any type of key is possible.

```smart header="`map[key]` isn't the right way to use a `Map`"
Although `map[key]` also works, e.g. we can set `map[key] = 2`, this is treating `map` as a plain JavaScript object, so it implies all corresponding limitations (only string/symbol keys and so on).
````smart header="`map[key]` isnt the correct way to use a `Map`"
Although you can technically write `map[key] = value`, this does **not** store the data inside the `Map` collection.

So we should use `map` methods: `set`, `get` and so on.
Instead, it simply adds a regular property to the `Map` object itself—just like with plain JavaScript objects—and the key is always converted to a string.

For example:

```js run
let map = new Map();
map["1"] = "test"; // adds a property, not a Map entry

alert(map.get("1")); // undefined
alert(map["1"]); // "test"
```

To properly store and retrieve data in a `Map`, always use its methods:
`map.set(key, value)` and `map.get(key)`.
````

**Map can also use objects as keys.**

For instance:
Expand Down

AltStyle によって変換されたページ (->オリジナル) /