.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: true
console.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.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy