Map.has() function in JavaScript


The has() function of Map object accepts a key in string format and returns a boolean value, if specified key exists it returns true else returns false.

Syntax

Its Syntax is as follows

mapVar.has()

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Java');
      mapVar.set('2', 'JavaFX');
      mapVar.set('3', 'HBase');
      mapVar.set('4', 'Neo4j');
      document.write(mapVar.has('3'));
   </script>
</body>
</html>

Output

true

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var mapVar = new Map();
      mapVar.set('1', 'Java');
      mapVar.set('2', 'JavaFX');
      mapVar.set('3', 'HBase');
      mapVar.set('4', 'Neo4j');
      document.write(mapVar.has('19'));
   </script>
</body>
</html>

Output

false

Updated on: 25-Jun-2020

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements