JavaScript weakMap has() Method

JavaScript weakMap has() method is used to return a boolean value(true or false) which indicates whether an element with a specified key exists in the weakmap object or not. 

Syntax

weakMap.has(key);

Parameters

key: The key of the element that is going to be tested for presence in the weakMap object.

Return Value

It returns a boolean value.

Example 1: How to Use JavaScript weakMap has() Method

let weakMap = new WeakMap();
let key1 = {};
let key2 = {};

weakMap.set(key1, 79);
weakMap.set(key2, 'askJavascript');


console.log(weakMap.has(key1));
console.log(weakMap.has(key2));

Output

true
true

Example 2

let weakMap = new WeakMap();
let key1 = {};
let key2 = {};

weakMap.set(key1, 79);


console.log(weakMap.has(key1));
console.log(weakMap.has(key2));

console.

Output

true
false

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript WeakMap

JavaScript WeakMap set() Method

JavaScript WeakMap get() Method

JavaScript WeakMap delete() Method

Leave a Comment