Sass map.has-key()

Beginner
10 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

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

SCSS
@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.

has-key vs get

Pick the helper based on whether you need existence or the stored value:

GoalUse
Does this key exist?map.has-key($map, $key)
What is the value?map.get($map, $key)
Nested path checkmap.has-key($map, $k1, $k2, …)
Key stores false / 0 / nullhas-key → true; get → that value
Default with orRisky for falsy tokens — prefer has-key first
Conditional CSS@if map.has-key(...)
Same path rules as get.

Earlier keys navigate nested maps; the last key is the one whose presence is tested.

SCSS Examples

Examples target Dart Sass with @use "sass:map". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Flat Key Check (Official Docs)

Ask whether a top-level key is present.

SCSS
@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")};
}

How It Works

  1. regular exists → true.
  2. bolder is absent → false.
  3. Return type is boolean, not the stored weight.
  4. Matches the official Sass docs sample.

Example 2 — Nested Path (Official Docs)

Pass multiple keys to test a nested entry.

SCSS
@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")};
}

How It Works

  1. Path walks Helvetica → weights → regular.
  2. That leaf exists → true.
  3. Missing colors branch → false.
  4. Matches the official nested-path sample.

Example 3 — Falsy Values Still “Have” a Key

Existence is separate from truthiness.

SCSS
@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};
}

How It Works

  1. debug exists even though its value is false.
  2. or ignores that stored false and takes the fallback.
  3. null and 0 keys still report has-key: true.
  4. Use has-key when falsy tokens are intentional.

Example 4 — Guard Reads with @if

Emit CSS only when a nested token is present.

SCSS
@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;
  }
}

How It Works

  1. accent exists → custom property is emitted.
  2. ghost is missing → @else branch runs.
  3. Safe pattern for optional theme keys.
  4. Prefer @if over deprecated plain if() for control flow.

Example 5 — Optional Component Tokens

Share one map; enable features only when keys are configured.

SCSS
@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);
  }
}

How It Works

  1. Required tokens are read with map.get.
  2. shadow exists → optional custom property appears.
  3. outline is absent → no outline rule is compiled.
  4. 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.

Sass Support

Prefer the module form with a current Dart Sass release. Nested multi-key checks need Dart Sass.

APIStatus
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 typetrue or false

Compile with the Dart Sass CLI.

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

Compile Notes

Practical checklist:

checklist.txt
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

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

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful