Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Maps

The Map Object

AJavaScript Map is an object that can store collections of key-value pairs,similar to adictionary in other programming languages.

Maps differ from standard objects in thatkeys can be of any data type.

Map Characteristics

  • Key Types
    Map keys can be any type (strings, numbers, objects, etc).

  • Insertion Order
    The Map remembers the original insertion order of the keys.

  • Size
    The number of items in a Map is easily retrieved using the size property.

  • Performance
    Maps are optimized for frequent additions and removals of key-value pairs.

  • Iteration
    Maps are iterable, allowing for direct use of for...of loops or methods like forEach().

  • Iteration Order
    The original order is preserved during iteration.


Note

Maps are similar to both Objects (unique key/value collection) and Arrays (ordered values collection).

But if you look close, Maps are most similar to Objects.


How to Create a Map

You create a JavaScript Map by:

  • Create a new Map and add elements withMap.set()
  • Passing an existing Array to thenew Map() constructor

Examples

Create a new Map and add elements withMap.set()

// Create an empty Map
const fruits = new Map();

// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
Try it Yourself »

Pass an Array to thenew Map() constructor:

// Create a Map
const fruits = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);
Try it Yourself »

Adding Map Values

You can add elements to a Map with theset() method:

Example

fruits.set("mangos", 100);
Try it Yourself »

Changing Map Values

Theset() method can also be used to change existing Map values:

Example

fruits.set("apples", 200);
Try it Yourself »

The get() Method

Theget() method gets the value of a key in a Map:

Example

fruits.get("apples");    // Returns 500
Try it Yourself »


Maps are Objects

Example

// Returns object:
typeof fruits;
Try it Yourself »

instanceof Map returns true:

Example

// Returns true:
fruits instanceof Map;
Try it Yourself »

JavaScript Objects vs Maps

Differences between JavaScript Objects and Maps:

ObjectMap
Not directly iterableDirectly iterable
Do not have a size propertyHave a size property
Keys must be Strings (or Symbols)Keys can be any datatype
Keys are not well orderedKeys are ordered by insertion
Have default keysDo not have default keys


Browser Support

Map is anES6 feature.

ES6 is fully supported in all modern browsers since June 2017:

Chrome
51
Edge
15
Firefox
54
Safari
10
Opera
38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp