JavaScript .has()
Published Dec 31, 2021Updated Jun 26, 2023
Contribute to Docs
Returns a boolean reflecting whether an entry with a given key exists in a Map object.
Syntax
The .has() method accepts a key as a parameter:
map.has(key);
If the key exists in map, .has() returns true. Otherwise, it returns false.
Example
Checking whether a key is in the fruits Map object can be done using the .has() method:
const fruits = new Map([['Apples', 5],['Oranges', 8],]);console.log(fruits.has('Apples')); // Output: trueconsole.log(fruits.has('Bananas')); // Output: false
The first .has() statement checks that there are 'Apples' in fruits, which returns true. The second statement finds that there are no 'Bananas' in fruits and false is returned.
Codebyte Example
The following example checks if there is a known abbreviation for the strings Typescript and Rust.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours