How To Solve “TypeError map.has Is Not A Function” In JavaScript

TypeError: map.has is not a function in JavaScript

Are you finding a way to solve the TypeError: map.has is not a function in JavaScript? Let’s follow this article. We will help you to fix it.

The cause of this error

The main reason for the TypeError: map.has is not a function in JavaScript is caused by when you create the map, but we did not define the value for the map, then we call the has() method on this map. This will show you an error.                                                       

Example:

// Create the map with no value 
const map = {};

// Call the has() method in this map(). Here is the cause of this error
const check = map.has('studentName');
console.log("Check the studentName exists on the map: ", check);

Output

The error will show if you try to run this code.

TypeError: map.has is not a function

How to fix the TypeError: map.has is not a function in JavaScript?

We’ve tested effectively with two solutions to solve the TypeError: map.has is not a function in JavaScript by creating the value for the map before calling the has() method and adding value to the map. Reading to the end of the article to see how we solve this problem.

Creating the value for the map before calling the has() method

To avoid this error, you can make sure your map has some objects. Then we just call the has() method to check if an element with the specified key exists or not on the map.

Syntax:

mapObject.has(key)

Parameter:

  • key: The key check of the element in the map.

Return Value: the return value is true if an element exists in the map; otherwise false.

Example:

// Create the Map() with information of student
const map = new Map([
	['studentID', '17T1021094'],
	['studentName', 'LearnShareIT'],
	['score', 10],
]);

// Check if the studentName value exists in the map
const check = map.has('studentName');
console.log("Check the value exists on the map: ", check);

Output

Check the value exists on the map: true

Adding the value to the map

The other simple way to solve this error is we create an empty map(). Then, we call the set() method to add value to map(). And finally, we call the has() method to check if the key exists in the map.

Let’s see the example below.

Example:

var map = {};

// Create the empty map() 
map = new Map();

// Then call the map.set it to adding value 
map.set('studentID', '17T1021094');
map.set('studentName', 'LearnShareIT');
map.set('score', 10);

// Using the has() method. Will return true because studentName exists on the map
const check = map.has('studentName');
console.log("Check the studentName exists on the map: ", check);

Output

Check the studentName exists on the map: true

Summary

In this article, we already explained to you how to solve the error TypeError: map.has is not a function in JavaScript with two solutions, but we think creating the value for a map before the call has() method is an easy and simple way to solve this error. We always hope this information will be of some help to you. If you have any questions, don’t hesitate and leave your comment below.

Thank you for your read!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *