The has()
function of the map object accepts a key in string format and returns a boolean value of true
if the specified key exists. Otherwise, the function returns false
.
Map is a data structure in JavaScript that allows the storage of [key, value] pairs where any value can be used as either a key or value.
mapObject.has(key)
The has()
function takes a key in the string format. It returns a boolean value that indicates the presence of that key in the Map object.
The code below illustrates the use of the has()
function in Javascript:
// creating a map objectvar myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);// checking for a key that exists in the map. Should// return trueconsole.log(myMap.has('c'))// checking for a key that does not exists in the map.// Should return falseconsole.log(myMap.has('w'))