Sass map.has-key() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:map · nested keys 1.27+

What You’ll Learn

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

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.

📝 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

ParameterTypeRequiredDescription
$mapMapYesMap to inspect. Not modified.
$keyAnyYesFirst key. Alone, this is the top-level key to test.
$keys...Any (variadic)NoExtra keys for a nested path. The last key is the one tested (Dart Sass 1.27+).

Return value

TypeResult
trueThe key (or nested last key) exists in the targeted map
falseMissing key, or any nested step is missing / not a map

📦 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.

🔍 Flat Check vs Nested Path

Official behavior matches map.get’s path rules, but returns a boolean:

  • No extra keystrue if $key is in $map
  • Extra keys — walk nested maps; test the last key; false if any step fails
styles.scss
@use "sass:map";

// Flat
map.has-key($font-weights, "regular"); // true

// Nested path: Helvetica → weights → regular
map.has-key($fonts, "Helvetica", "weights", "regular"); // true

📋 map.has-key vs map.get

This is the key beginner trap: a stored false or null is still a present key.

map.has-keymap.get
Returnstrue / falseValue or null
Best for@if guards, optional branchesEmitting token values
Key exists, value is falsetruefalse
Key missingfalsenull
Nested keysDart Sass 1.27+Dart Sass 1.27+
💡
Typical pattern

@if map.has-key($map, $key) { $value: map.get($map, $key); } — confirm first, then read.

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the finished values you emit after the check.

ImplementationFlat has-key / map-has-keyNested multi-key
Dart SassYes (@use since 1.23+)Yes since 1.27.0
LibSassUse global map-has-keyNo nested multi-key API
Ruby SassLegacy map-has-keyNo—prefer Dart Sass

⚡ Quick Reference

GoalCode
Import map@use "sass:map";
Top-level checkmap.has-key($map, "regular")
Nested checkmap.has-key($map, "a", "b", "c")
Guard then read@if map.has-key(...) { map.get(...) }
Legacy namemap-has-key($map, $key)
Nested keys sinceDart Sass 1.27+

Examples Gallery

Each example uses @use "sass:map". Open View Compiled CSS for verified Dart Sass output (official samples where noted).

📚 Getting Started

Official docs samples: flat and nested has-key.

Example 1 — Top-Level Key (Official Docs)

Ask whether a weight name is listed in the map.

styles.scss
@use "sass:map";

$font-weights: (
  "regular": 400,
  "medium": 500,
  "bold": 700
);

@debug map.has-key($font-weights, "regular"); // true
@debug map.has-key($font-weights, "bolder"); // false

.weights {
  --regular: #{map.has-key($font-weights, "regular")};
  --bolder: #{map.has-key($font-weights, "bolder")};
}

How It Works

"regular" is present → true. "bolder" is not → false. No values are returned—only existence.

Example 2 — Nested Path (Official Docs)

Check a deep weight key, then a missing nested branch.

styles.scss
@use "sass:map";

$fonts: (
  "Helvetica": (
    "weights": (
      "regular": 400,
      "medium": 500,
      "bold": 700
    )
  )
);

@debug map.has-key($fonts, "Helvetica", "weights", "regular"); // true
@debug map.has-key($fonts, "Helvetica", "colors"); // false

.fonts {
  --regular: #{map.has-key($fonts, "Helvetica", "weights", "regular")};
  --colors: #{map.has-key($fonts, "Helvetica", "colors")};
}

How It Works

The path Helvetica → weights → regular succeeds. Asking for colors under Helvetica fails and returns false.

📈 Practical Patterns

Compare with get, @if guards, and broken paths.

Example 3 — Existence vs Value

See why false / null values still count as present keys.

styles.scss
@use "sass:map";
@use "sass:meta";

$config: (
  "enabled": false,
  "label": null
);

.compare {
  --has-enabled: #{map.has-key($config, "enabled")};
  --get-enabled: #{meta.inspect(map.get($config, "enabled"))};
  --has-label: #{map.has-key($config, "label")};
  --get-label: #{meta.inspect(map.get($config, "label"))};
  --has-ghost: #{map.has-key($config, "ghost")};
  --get-ghost: #{meta.inspect(map.get($config, "ghost"))};
}

How It Works

enabled and label exist (has-key → true) even though get returns false / null. ghost is missing on both helpers.

Example 4 — Optional Theme Tokens with @if

Emit a color only when present; supply a default when a key is absent.

styles.scss
@use "sass:map";

$theme: (
  "colors": (
    "primary": #336699,
    "accent": #e67e22
  ),
  "radius": 8px
);

.theme {
  @if map.has-key($theme, "colors", "accent") {
    --accent: #{map.get($theme, "colors", "accent")};
  }
  @if not map.has-key($theme, "shadow") {
    --shadow: none;
  }
  --has-radius: #{map.has-key($theme, "radius")};
}

How It Works

Nested has-key unlocks the accent variable. Missing shadow triggers a fallback. Flat has-key confirms radius.

Example 5 — Broken Nested Path

If a middle value is not a map, nested has-key returns false.

styles.scss
@use "sass:map";

$flat: (
  "token": 1rem
);

.broken {
  --deep: #{map.has-key($flat, "token", "size")};
}

How It Works

token holds a length, not a map, so digging for size fails and returns false—same idea as nested get returning null.

🚀 Real-World Use Cases

  • Optional theme tokens — emit CSS variables only when a key exists.
  • Feature flags in maps — distinguish “key present and false” from “key missing”.
  • Mixin APIs — accept a config map and branch on which options were passed.
  • Safe nested reads — check the path with has-key before get.
  • Typography scales — verify a weight exists before applying it (docs sample).

🧠 How Compilation Works

1

Write SCSS

Call map.has-key($map, $key, $keys...) after @use "sass:map".

Source
2

Walk the path

Dart Sass checks the key or nested maps for the last key.

Compile
3

Return a boolean

Use the result in @if, or emit it for debugging.

Result
4

CSS sees branches

Only the rules you chose to emit remain in the stylesheet.

⚠️ Common Pitfalls

  • Using get as a boolean — a stored false looks “off” even though the key exists.
  • Assuming nested keys everywhere — multi-key has-key needs Dart Sass 1.27+.
  • Expecting the value backhas-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.

💡 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

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
📦 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.

Conclusion

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.

Continue with map.keys() or the Sass introduction.

Next: list map keys

Learn map.keys() to loop names and build utility classes.

map.keys() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful