Getting the value associated with the key(get method)
Now, we have the key-value pairs on the Map, and now we are going to concentrate on how can we get the value associated with the key from the Map. We got a method for this, which is the get method. With this method, we just have to specify for what key, do we need the value? Means value associated with which key, that key needs to be passed here, and then we are going to get the value of the corresponding key –
JavaScript Map get Method
Let’s have a look at a simple example, through which, we can understand the get method here.
As you can see, we want to get the value associated with key 1. So, the output that we get, is JavaScript(which is the corresponding value). Have a look at the output now –
As you can see, we got the value corresponding to key 1, which is JavaScript. You can also try some other keys here, with the get method.
Now, what if we give such a key, which is not present on the Map? Well, you can try doing this. It is going to give undefined in such a case.