JavaScript Map get() Method Tutorial

In this section, we will learn what the get() method is and how to use it in JavaScript.

What is JavaScript Map get() Method?

The `get()` method is used to get the value associated with a key in a Map object.

Basically, this method takes a key as its argument and looks for that key in a map object. Now if it can find that key in the map, then its value will be returned. (Otherwise the value undefined returns instead).

JavaScript Map get() Method Syntax:

get(key);

get() Function Parameters:

This method takes one argument, and that is the name of the key in which we want its value.

Note: if the key does not exist in the target map, the return value of this method will be `undefined`.

get() Function Return Value:

The return value of this method is the value associated with the target key (that we set as the argument to the method) in a map object.

Note: if the key does not exist in the target map object, the return value of this method becomes undefined.

Example: using Map get() method in JavaScript

const map = new Map();

map.set("John","222-22222222");

map.set(22 , 2000);

map.set(true, false);

map.set("Jack", "333-3234232");

console.log(map.get("John"));

console.log(map.get(22));

//The return value of the next two statements

// is undefined.

console.log(map.get("pure"));

console.log(map.get(1000))

Output:

222-22222222

2000

undefined

undefined

As you can see, the last two statements returned `undefined`. This is because the keys `pure` and `1000` don’t exist on this map.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies