Updated April 3, 2023
Introduction to JavaScript has()
has() is a function of the map object in JavaScript. It accepts a key in string format and returns a value in a Boolean format of true if that specific key exists otherwise it will return a false value. has() in JavaScript is a very important function of the map as a data structure because this specific data structure can only support the has functionality with an ability to provide key and value pairs. The string which will be provided to this function will be in the form of a string and the corresponding return value to it will be in the Boolean format.
Syntax with Parameters:
mapObject.has(key)
has() is a function with a key as its parameter which will be passed in the string format to the function and then it will be finally called with the associated map Object which indicates the presence of the key in that map data structure. Map object key in it will have its key pairs which will return the Boolean value.
How has() method works in JavaScript?
The working of has method() in JavaScript revolves around the same fact of key and value pair in a way it will be used to return the Boolean value of true and false for the satisfied key value to the required data structure of Map passing the value to the has object as part of Map data structure. The map data structure has an iterative approach to be followed where the map can add in the map collection if any new element comes in the picture that gets overridden with the new value and the old value becomes useless. Elements get designed in insertion order and a for loop returns an array of all[key and value pair]. Although the functionality of the map and object Is the same in JavaScript but still, they have differences in them Object contains very limited symbols and strings, the size of the map can be easily known by size properties but while dealing with the objects it must determine the size manually. If in case it becomes a frequent need to make all the data insertion and retrieval then it is better to use Map instead of the object because the object makes use of obtaining keys in a specific manner only. In short, the has() method works for checking whether the element with a specified key exists or not.
Examples of JavaScript has()
Here are the following examples mentioned below:
Example #1
To check for the variable whether the element gets existed for the set of computer science set Example based on the DataScience and to return the type of Boolean value as false.
Code:
<html>
<head>
<title>Computer Science Set Example</title>
</head>
<body>
<script type="text/javascript">
var mapVar = new Map();
mapVar.set('1', 'DataScience');
mapVar.set('2', 'JavaFX');
mapVar.set('3', 'HTML 5');
mapVar.set('4', 'DevOps Ex');
mapVar.set('Machine_Learning', 'DevOps Ex');
document.write(mapVar.has('5'));
</script>
</body>
</html>
Output:
Explanation: In the above example the Map iterator list being described is sorted using insertion sort and the data or the elements are arranged in an incremental fashion. Therefore the elements set inside the map is needed to be called when the value wants to return a value like in this case the returned element is false which means that the object of has function inside the map data structure is not able to find the existing value, therefore, returning value as false.
Example #2
To check for the variable whether the element gets existed for the set of computer science set Example based on the DataScience and to return the type of Boolean value as true.
Code:
<html>
<head>
<title>Computer Science Set Example</title>
</head>
<body>
<script type="text/javascript">
var mapVar = new Map();
mapVar.set('1', 'DataScience');
mapVar.set('2', 'JavaFX');
mapVar.set('3', 'HTML 5');
mapVar.set('4', 'DevOps Ex');
mapVar.set('Machine_Learning', 'DevOps Ex');
document.write(mapVar.has('3'));
</script>
</body>
</html>
Output:
Explanation: In the above example the existing list comprises of a set of map values which is existing in the list of the hash maps and the object due to which it is able to tell the existence of the set of values and return type as Boolean.
Example #3
This Example will check for the Boolean returned values for the set of alphabets present in the Map defined structures and values with variables returning value as false.
Code:
<html>
<head>
<title>Presence of Alphabet Example</title>
</head>
<body>
<script type="text/javascript">
var mapVar = new Map();
var myMap = new Map([['x', 11], ['y', 12], ['z', 33]]);
console.log(myMap.has('y'))
console.log(myMap.has('x'))
document.write(mapVar.has('3'));
</script>
</body>
</html>
Output:
Example #4
This Example will check for the Boolean returned values for the set of alphabets present in the Map defined structures and values with returned value as false.
Code:
<html>
<head>
<title>Computer Science Set Example</title>
</head>
<body>
<script type="text/javascript">
var mapVar = new Map();
var myMap = new Map([['x', 11], ['y', 12], ['z', 33]]);
console.log(myMap.has('y'))
console.log(myMap.has('x'))
document.write(mapVar.has('11'));
</script>
</body>
</html>
Output:
Example #5
Creating an object with key-value and pairs and then finding after traversing whether the key and element exists within that specific Map set or not.
Code:
<html>
<head>
<script type="text/javascript">
function returnSum(frst, scond)
{
var sum = frst + scond;
return sum;
}
var frstNo = 15;
var scondNo = 18;
document.write(frstNo + " + " + scondNo + " = " + returnSum(frstNo,scondNo));
</script>
</head>
<body>
</body>
</html>
Output:
Explanation: In the above example two numbers are taken as an input and then those two numbers are kept in the Map table to store the sum in the variable with the table to get and return the sum as some value to it. function return to the sum can be defined with the has list as well for writing and printing the value to it.
Advantages of using has() in JavaScript
- has() function in JavaScript has its own unique feature in a way that it has a special data structure as Map that helps the has method() to arrange the data in an incremental and ordered manner.
- Has an object present in the JavaScript helps in pointing and specifying the exact element on top of which any manipulation is possible.
- Has() map also helps in frequent retrieval and traversing of elements within the list of objects and maps.
- Key and Value pair management of insertion, retrieval, and deletion gets simplified with has() function in JavaScript.
Conclusion
Has() function can get the key and value pair arranged in a way that it will return some Boolean value based on some conditions of map and object which in turn have their own unique way to make all the adjustments necessary.
Recommended Articles
This is a guide to JavaScript has(). Here we discuss the Examples of JavaScript has() and How it works in JavaScript. You may also have a look at the following articles to learn more –