The has method of the JavaScript Map object determines if a specific key exists within the Map. It yields a true value if the key in question is found, and false if it is not present.
Syntax
The syntax for the has method is illustrated as follows:
Example
mapObj.has(key)
Parameter
key - It represents the key to be searched.
Return
A Boolean value.
JavaScript Map has method example
In this section, we will explore the has method by examining a variety of examples.
Example 1
Let us examine an example to ascertain if the map object includes the given key.
Example
<script>
var map=new Map();
map.set(1,"jQuery");
map.set(2,"AngularJS");
map.set(3,"Bootstrap");
document.writeln(map.has(2));
</script>
Output:
Example 2
Let us examine an additional example to ascertain if the map object holds the given key.
Example
<script>
var map=new Map();
map.set(1,"jQuery");
map.set(2,"AngularJS");
map.set(3,"Bootstrap");
document.writeln(map.has(5));
</script>
Output:
Example 3
Let's examine the outcome when the has method is invoked without providing a specific key.
Example
<script>
var map=new Map();
map.set(1,"jQuery");
map.set(2,"AngularJS");
map.set(3,"Bootstrap");
document.writeln(map.has()+"<br>");
document.writeln(map.has("jQuery"));
</script>
Output:
Output
false
false