.get()

Retrieves a value associated with a given key in a Map object.

Syntax

The .get() method accepts a key parameter and checks it against a Map object. If the key exists in the map, the accosiated value is returned. If not, undefined is returned.

map.get(key);

Example

Values are retrieved by their keys from Map objects using the .get() method.

const fruits = new Map([
['Apples', 5],
['Oranges', 8],
]);
console.log(fruits.get('Apples')); // Output: 5
console.log(fruits.get('Oranges')); // Output: 8
console.log(fruits.get('Bananas')); // Output: undefined

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy