JavaScript Map has() method - JavaScript Tutorial

JavaScript Map has() method

BLUF: This tutorial on JavaScript Map has() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Map has() method

Understanding JavaScript Map has() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The has method of the JavaScript Map object determines if a specific key exists within the Map. It yields a true value if the key in question is found, and false if it is not present.

Syntax

The syntax for the has method is illustrated as follows:

Example

mapObj.has(key)

Parameter

key - It represents the key to be searched.

Return

A Boolean value.

JavaScript Map has method example

In this section, we will explore the has method by examining a variety of examples.

Example 1

Let us examine an example to ascertain if the map object includes the given key.

Example

<script>

var map=new Map();

map.set(1,"jQuery");

map.set(2,"AngularJS");

map.set(3,"Bootstrap");

document.writeln(map.has(2)); 

</script>

Output:

Example 2

Let us examine an additional example to ascertain if the map object holds the given key.

Example

<script>

var map=new Map();

map.set(1,"jQuery");

map.set(2,"AngularJS");

map.set(3,"Bootstrap");

document.writeln(map.has(5)); 

</script>

Output:

Example 3

Let's examine the outcome when the has method is invoked without providing a specific key.

Example

<script>

var map=new Map();

map.set(1,"jQuery");

map.set(2,"AngularJS");

map.set(3,"Bootstrap");

document.writeln(map.has()+"<br>");

document.writeln(map.has("jQuery")); 

</script>

Output:

Output

false

false

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience