JavaScript .get()
Published Dec 31, 2021Updated May 15, 2024
Contribute to Docs
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 associated 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: 5console.log(fruits.get('Oranges')); // Output: 8console.log(fruits.get('Bananas')); // Output: undefined
Codebyte Example
The code below uses a new Map that contains purchase orders.
In this example:
- Samantha will
getthe next purchase order that she needs to fill from the map using the.get()method. - Once the purchase order has been worked, Samantha will remove it from the map using the
.delete()method.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours