Map.prototype.has()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Map
实例的 has()
方法返回一个布尔值,指示具有指定键的元素是否存在。
尝试一下
const map1 = new Map();
map1.set("bar", "foo");
console.log(map1.has("bar"));
// Expected output: true
console.log(map1.has("baz"));
// Expected output: false
语法
js
has(key)
参数
key
-
用于测试
Map
对象中是否存在的元素的键。
返回值
如果 Map
对象中存在具有指定键的元素,则返回 true
;否则返回 false
。
示例
使用 has()
js
const myMap = new Map();
myMap.set("bar", "foo");
console.log(myMap.has("bar")); // true
console.log(myMap.has("baz")); // false
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-map.prototype.has |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
has |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.