Home Sass Map map.has-key() Sass map.has-key() Definition
Definition and Usage map.has-key($map, $key, $keys...) returns true if the map contains that key, otherwise false . With extra keys it checks a nested path the same way map.get walks nested maps. Load it with @use "sass:map" on Dart Sass 1.23+ (nested multi-key paths need a current Dart Sass release). Legacy global name: map-has-key.
Official samples. map.has-key(("regular": 400, "medium": 500, "bold": 700), "regular") → true. Missing "bolder" → false. Nested: map.has-key($fonts, "Helvetica", "weights", "regular") → true.
Existence, not value. A key can exist and still store false, 0, or null. has-key stays true; map.get returns that falsy value.
Broken nested path → false. Official docs: if any key is missing or points at a non-map before the end, has-key returns false.
Syntax
Syntax @use "sass:map";
map.has-key($map, $key); //=> boolean
map.has-key($map, $key, $keys...); // nested path (Dart Sass)
map.has-key($font-weights, "regular"); // true
map.has-key($font-weights, "bolder"); // false
map.has-key($fonts, "Helvetica", "weights", "regular"); // true
// Legacy global (prefer module form)
// map-has-key($map, $key); Pattern: map.has-key to guard → map.get to read → optional @if for conditional CSS.
Code
SCSS Examples Examples target Dart Sass with @use "sass:map". Open View Compiled CSS for verified output (matches official Sass docs samples).
1. Basic 2. Nested path 3. Falsy values 4. @if guard 5. Optional tokens Example 1 — Flat Key Check (Official Docs) Ask whether a top-level key is present.
@use "sass:map";
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
.flat {
--regular: #{map.has-key($font-weights, "regular")};
--bolder: #{map.has-key($font-weights, "bolder")};
} .flat {
--regular: true;
--bolder: false;
} How It Works regular exists → true.bolder is absent → false.Return type is boolean , not the stored weight.Matches the official Sass docs sample.Example 2 — Nested Path (Official Docs) Pass multiple keys to test a nested entry.
@use "sass:map";
$fonts: (
"Helvetica": (
"weights": (
"regular": 400,
"medium": 500,
"bold": 700
)
)
);
.nested {
--regular: #{map.has-key($fonts, "Helvetica", "weights", "regular")};
--colors: #{map.has-key($fonts, "Helvetica", "colors")};
} .nested {
--regular: true;
--colors: false;
} How It Works Path walks Helvetica → weights → regular.That leaf exists → true.Missing colors branch → false.Matches the official nested-path sample.Example 3 — Falsy Values Still “Have” a Key Existence is separate from truthiness.
@use "sass:map";
@use "sass:meta";
$flags: (debug: false, verbose: null, count: 0);
.falsy {
--debug-has: #{map.has-key($flags, debug)};
--debug-get: #{meta.inspect(map.get($flags, debug))};
--verbose-has: #{map.has-key($flags, verbose)};
--count-has: #{map.has-key($flags, count)};
--or-debug: #{map.get($flags, debug) or true};
} .falsy {
--debug-has: true;
--debug-get: false;
--verbose-has: true;
--count-has: true;
--or-debug: true;
} How It Works debug exists even though its value is false.or ignores that stored false and takes the fallback.null and 0 keys still report has-key: true.Use has-key when falsy tokens are intentional.Example 4 — Guard Reads with @if Emit CSS only when a nested token is present.
@use "sass:map";
$theme: (colors: (primary: navy, accent: teal));
.guard {
@if map.has-key($theme, colors, accent) {
--accent: #{map.get($theme, colors, accent)};
}
@if map.has-key($theme, colors, ghost) {
--ghost: yes;
} @else {
--ghost: no;
}
} .guard {
--accent: teal;
--ghost: no;
} How It Works accent exists → custom property is emitted.ghost is missing → @else branch runs.Safe pattern for optional theme keys.Prefer @if over deprecated plain if() for control flow.Example 5 — Optional Component Tokens Share one map; enable features only when keys are configured.
@use "sass:map";
$button: (
pad: 8px,
radius: 4px,
shadow: true
);
.btn {
padding: map.get($button, pad);
border-radius: map.get($button, radius);
@if map.has-key($button, shadow) {
--shadow: #{map.get($button, shadow)};
}
@if map.has-key($button, outline) {
outline: map.get($button, outline);
}
} .btn {
padding: 8px;
border-radius: 4px;
--shadow: true;
} How It Works Required tokens are read with map.get.shadow exists → optional custom property appears.outline is absent → no outline rule is compiled.Keeps component APIs flexible without empty declarations. Edge Cases & Pitfalls Most has-key bugs are confusing it with get, or trusting or for falsy tokens.
falsy false / 0 / null can still exist has-key answers presence. Use get only after you know the key is there—or when null is an acceptable miss.
or Defaults hide real falsy values map.get($m, debug) or true turns a stored false into true. Check has-key first.
path Non-map mid-path → false If an intermediate key holds a color or number, deeper checks return false.
legacy Prefer map.has-key over map-has-key Global map-has-key still works but is deprecated toward Dart Sass 3.0. Use the module API.
Toolchain
Sass Support Prefer the module form with a current Dart Sass release. Nested multi-key checks need Dart Sass.
API Status map.has-key()Dart Sass 1.23+ via @use "sass:map" Nested $keys... Dart Sass (multi-argument has-key) Global map-has-key() Works; deprecated toward Sass 3.0 Return type true or false
Compile with the Dart Sass CLI .
Remember
Key Takeaways Returns true/false for key or nested-path existence.
Safer than get + or when values can be falsy.
Pair with @if for optional theme/CSS output.
Prefer map.has-key over deprecated global map-has-key.
One line: when you need to know if a key exists, call map.has-key($map, $key) or map.has-key($map, $path...).
Practice
Compile Notes Practical checklist:
1. @use "sass:map"
2. Use has-key for existence; get for values
3. Nested tokens: map.has-key($map, group, token)
4. Guard optional CSS with @if map.has-key(...)
5. Avoid global map-has-key in new code Frequently Asked Questions What does Sass map.has-key() do? map.has-key($map, $key, $keys...) returns true if the map has that key, otherwise false. With extra keys (Dart Sass) it checks a nested path the same way map.get walks nested maps.
How is has-key different from map.get? 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, 0, or null and you still need to know if the key is present.
Is map-has-key still valid? Yes. map-has-key($map, $key) is the older global name, but Dart Sass deprecates global built-ins toward Sass 3.0. Prefer map.has-key after @use "sass:map". Nested multi-key checks need the module API.
What if a middle key is not a map? 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.
Does has-key change the map? No. It only checks. Pair it with map.get to read values, or with @if to emit optional CSS.
Which compilers support nested has-key? Flat two-argument checks work widely via map.has-key / map-has-key. Multi-key nested checks are Dart Sass only.
Next: Sass map.keys() Get a comma-separated list of every key in a map.
map.keys() → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion