Movatterモバイル変換


[0]ホーム

URL:


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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ayush007-git wants to merge2 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromayush007-git:patch-1
Open
Changes fromall 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 deletions1-js/05-data-types/07-map-set/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 theright way to use a `Map`"
Although`map[key]` also works, e.g. wecanset`map[key] =2`, thisis 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 thecorrect way to use a `Map`"
Althoughyoucantechnically write`map[key] =value`, thisdoes **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

[8]ページ先頭

©2009-2025 Movatter.jp