What is Map.has() function in Javascript?

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.

Syntax

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.

svg viewer

Code

The code below illustrates the use of the has() function in Javascript:

// creating a map object
var myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);
// checking for a key that exists in the map. Should
// return true
console.log(myMap.has('c'))
// checking for a key that does not exists in the map.
// Should return false
console.log(myMap.has('w'))
Copyright ©2024 Educative, Inc. All rights reserved