map.has-key() belongs to the built-in sass:map module. It answers a yes/no question: does this key (or nested path) exist? This page covers true/false results, vs map.get, nested checks (Dart Sass 1.27+), @if guards, and five compiled examples.
01
Concept
Key exists?
02
Module
@use "sass:map"
03
Returns
true / false
04
vs get
Existence vs value
05
Nested
Multi-key path
06
Practice
5 examples
Concept
What Is map.has-key()?
Theme maps often have optional tokens. Before you emit a CSS variable, you may want to know whether a key is present—even if its value is false or null. Official docs: with only $key, has-key returns whether the map contains that top-level key. With extra keys, it walks nested maps and checks the last key.
One key → top-level existence check
Several keys → nested path check (Dart Sass 1.27+)
Always returns a boolean—never the stored value
💡
Beginner tip
Think of a checklist: map.has-key($weights, "bold") asks “is bold listed?” map.get asks “what is bold’s number?” Use both together in real themes.
Foundation
📝 Syntax
Load the map module, then call the function:
styles.scss
@use "sass:map";
map.has-key($map, $key, $keys...)
// Legacy global name (still works)
map-has-key($map, $key)
Parameters
Parameter
Type
Required
Description
$map
Map
Yes
Map to inspect. Not modified.
$key
Any
Yes
First key. Alone, this is the top-level key to test.
$keys...
Any (variadic)
No
Extra keys for a nested path. The last key is the one tested (Dart Sass 1.27+).
Return value
Type
Result
true
The key (or nested last key) exists in the targeted map
false
Missing key, or any nested step is missing / not a map
Modules
📦 Loading sass:map
styles.scss
// Recommended — namespaced
@use "sass:map";
$ok: map.has-key($theme, "colors", "primary");
// Optional — bring members into scope
@use "sass:map" as *;
$ok: has-key($theme, "colors", "primary");
// Optional — custom namespace
@use "sass:map" as m;
$ok: m.has-key($theme, "colors", "primary");
// Legacy global (flat keys)
$ok: map-has-key($font-weights, "regular");
⚠️
Module vs legacy
Prefer map.has-key with @use "sass:map". Keep map-has-key for older snippets. Nested multi-key checks need modern Dart Sass and the module API.
Details
🔍 Flat Check vs Nested Path
Official behavior matches map.get’s path rules, but returns a boolean:
No extra keys — true if $key is in $map
Extra keys — walk nested maps; test the last key; false if any step fails
Expecting the value back — has-key only returns true/false; use get to read.
Broken middle keys — if a path step is not a map, the result is false.
Forgetting quotes — string keys must match how the map was written.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:map" and map.has-key()
Check existence before reading optional tokens
Use @if map.has-key(...) for optional CSS
Pair with map.get when you need the value
Prefer Dart Sass 1.27+ for nested design-system maps
❌ Don’t
Expect the browser to evaluate map.has-key
Treat map.get(...) or ... as a perfect existence test for flags
Confuse a false value with a missing key
Rely on nested multi-key checks under LibSass
Forget that broken nested paths return false
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about map.has-key()
Boolean existence check—nested paths on Dart Sass 1.27+; pair with get.
5
Core concepts
📝01
Call
map.has-key($map, $key, $keys...)
API
📦02
Module
sass:map
@use
🔢03
Returns
true or false
Rule
❓04
vs get
existence vs value
Safe
✓05
Legacy
map-has-key works
Tooling
❓ Frequently Asked Questions
map.has-key($map, $key, $keys...) returns true if the map has that key, otherwise false. With extra keys (Dart Sass 1.27+) it checks a nested path the same way map.get walks nested maps.
map.get returns the stored value or null. map.has-key returns true or false for existence. Use has-key when the real value might be false or null and you still need to know if the key is present.
Yes. map-has-key($map, $key) is the older global name. Prefer map.has-key after @use "sass:map". Nested multi-key checks need Dart Sass 1.27+ and the module API.
Official docs: has-key returns false if any key in the path is missing or points to a value that is not a map—same idea as a failed nested get returning null.
No. It only checks. Pair it with map.get to read values, or with @if to emit optional CSS.
Flat two-argument checks work widely via map-has-key / map.has-key. Multi-key nested checks are Dart Sass 1.27+ only.
Did you know?
Official Sass docs give map.has-key the same nested key-path style as map.get, map.set, and the deep helpers—so one mental model covers reading, writing, and checking design-system maps.
map.has-key() is the boolean twin of map.get—perfect for optional tokens and @if guards. Load it through sass:map, use nested paths on Dart Sass 1.27+, and remember that a present false is still a key.