Checking if the Map has the specified key (has method)
Now, let’s have a look at another method, which is going to help us find whether or not, some key is there on the map, associated with some value. So, we are going to give some key name as input, and then it is going to return true if the key is there, and it associates to some value, and false otherwise.
Here, the method that we are going to use, is the has method. In short, we are trying to check whether or not the map has that key.
Let’s now have a look at a simple program, through which, we can simply understand the use of the has method.
If we try to run the above program into the browser, the output comes out to be true, since key 1 is there in the Map, associated with some value. Otherwise, it is going to return false. Have a look at the below program as well, where, we are giving such a key, which is not there on the Map.
As you can see, this time, we are passing such a key to the has method, which is not there at all. In this case, the output comes out to be false. You can try having a look at the output as well.