EN
JavaScript - check if map contains key
0 points
In this article, we would like to show you how to check if a Map contains a key using JavaScript.
Quick solution:
xxxxxxxxxx
1
mapName.has('key');
In this example, we use has()
method to check if Map contains the specific key.
xxxxxxxxxx
1
const myMap = new Map([
2
['key1', 'value1'],
3
['key2', 'value2'],
4
['key3', 'value3'],
5
]);
6
7
console.log(myMap.has('key1')); // true
8
console.log(myMap.has('key4')); // false
Output:
xxxxxxxxxx
1
true
2
false