The has() method of the Map object in JavaScript returns a boolean indicating whether a value has been associated with the passed key in the Map object or not.
The syntax of Map.has() method is:
has(key)
This has() method of Map returns a boolean indicating whether a value has been associated with the passed key in the Map object or not.
Parameter | Optional/Required | Description |
---|---|---|
key | required | The key of the element to test for presence in the Map. |
Map.has() returns value of type boolean
.
In JavaScript, we can use the has()
method to check if a specific key is present in a Map object.
For example,
map
with initial key-value pairs.has()
method to check if the key 'a' is present in the map
.console.log()
.const map = new Map([['a', 1], ['b', 2]]);
const hasA = map.has('a');
console.log(hasA);
true
In JavaScript, we can use the has()
method to check if a key that is not present in the Map object returns false.
For example,
map
with initial key-value pairs.has()
method to check if the key 'c' is present in the map
.console.log()
.const map = new Map([['a', 1], ['b', 2]]);
const hasC = map.has('c');
console.log(hasC);
false
In JavaScript, we can use the has()
method to check if an object key is present in a Map object.
For example,
map
with initial key-value pairs where keys are objects.has()
method to check if a specific object key is present in the map
.console.log()
.const key1 = { id: 1 };
const key2 = { id: 2 };
const map = new Map([
[key1, 'value1'],
[key2, 'value2']
]);
const hasKey1 = map.has(key1);
console.log(hasKey1);
true
In this JavaScript tutorial, we learned about has() method of Map: the syntax and few working examples with output and detailed explanation for each example.