Reference · Maps

has

signature
has(map | null, any) -> bool

What it does

Whether a map has an entry under a key: true or false. The key is turned into text first, as all map keys are. An entry whose value is null counts as present, which is what tells has apart from get(m, k) != null.

Examples

let user = { name: "Ann", email: null }print(has(user, "name"), has(user, "phone"))print(has(user, "email"), get(user, "email") != null)
output
true falsetrue false

Edge cases

let m = { "5": "five" }print(has(m, 5), has(m, "5"), has(m, 6))print(has(null, "a"), has({}, ""))print(contains(m, "5"))print(has(m, int("q")))
output
true true falsefalse falsetrueerr("not an int: \"q\"")

Errors

A map that is not a map or null is a runtime error, not an error value. For a list, contains looks for an item.

this one fails
print(has(3, "a"))
output
line 1: `has` expects a map as argument 1, got int

Chaining

let m = { a: 1, b: 2 }print(m | has("a"))print(["a", "x", "b"] | filter((k) => has(m, k)))
output
true["a", "b"]

See also

get, keys, contains, set

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On has(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo