Sass map.get() Function

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

What You’ll Learn

map.get() belongs to the built-in sass:map module. It reads a value from a map by key—or by a nested key path. This page covers missing keys (null), map-get, nested lookup (Dart Sass 1.27+), defaults with or, and five compiled examples.

01

Concept

Read by key

02

Module

@use "sass:map"

03

Missing

Returns null

04

Nested

Multi-key path

05

Legacy

map-get

06

Practice

5 examples

What Is map.get()?

Maps store named values—font weights, colors, radii. map.get is how you pull one value out so you can put it in CSS. Official docs: with only $key, you get the value for that top-level key (or null if it is missing). With extra keys, Sass walks nested maps and returns the value at the last key.

  • One key → top-level lookup
  • Several keys → nested path (Dart Sass 1.27+)
  • Missing key or broken path → null, not an error
💡
Beginner tip

Think of a dictionary: map.get($weights, "medium") asks for the entry named medium. Nested keys are like folders: map.get($fonts, "Helvetica", "weights", "regular").

📝 Syntax

Load the map module, then call the function:

styles.scss
@use "sass:map";

map.get($map, $key, $keys...)

// Legacy global name (still works)
map-get($map, $key)

Parameters

ParameterTypeRequiredDescription
$mapMapYesMap to read from. Not modified.
$keyAnyYesFirst key. Alone, this is the top-level key to look up.
$keys...Any (variadic)NoExtra keys for nested lookup. The last key is the value you want (Dart Sass 1.27+).

Return value

TypeResult
AnyThe stored value when the key (or nested path) exists
nullWhen the key is missing, or a nested step is missing / not a map

📦 Loading sass:map

styles.scss
// Recommended — namespaced
@use "sass:map";
$primary: map.get($theme, "colors", "primary");

// Optional — bring members into scope
@use "sass:map" as *;
$primary: get($theme, "colors", "primary");

// Optional — custom namespace
@use "sass:map" as m;
$primary: m.get($theme, "colors", "primary");

// Legacy global (flat keys)
$medium: map-get($font-weights, "medium");
⚠️
Module vs legacy

Prefer map.get with @use "sass:map". Keep map-get for older snippets. Nested multi-key lookup is a modern Dart Sass feature—use the module API.

🔍 Flat Lookup vs Nested Path

Official behavior splits cleanly into two cases:

  • No extra keys — return the value for $key, or null
  • Extra keys — walk left to right through nested maps; return the value for the last key, or null if any step fails
styles.scss
@use "sass:map";

// Flat
map.get($font-weights, "medium"); // 500

// Nested path: Helvetica → weights → regular
map.get($fonts, "Helvetica", "weights", "regular"); // 400

📋 map.get vs map.has-key

Both accept the same key-path style. Choose based on whether you need the value or a yes/no answer.

map.getmap.has-key
ReturnsValue or nulltrue / false
Best forEmitting CSS tokensGuards and conditionals
Value is false or nullHard to tell from “missing” with get aloneClearly says if the key exists
Nested keysDart Sass 1.27+Dart Sass 1.27+
💡
Defaults with or

A common pattern: map.get($map, $key) or $fallback. Remember that false, null, and empty string are all falsy in Sass—so for flags that may be false, prefer map.has-key first.

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the finished values you read from the map.

ImplementationFlat map.get / map-getNested multi-key
Dart SassYes (@use since 1.23+)Yes since 1.27.0
LibSassUse global map-getNo nested multi-key API
Ruby SassLegacy map-getNo—prefer Dart Sass

⚡ Quick Reference

GoalCode
Import map@use "sass:map";
Read top-levelmap.get($map, "medium")
Read nestedmap.get($map, "a", "b", "c")
Default valuemap.get($map, $key) or $fallback
Key exists?map.has-key($map, $key)
Legacy namemap-get($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 get and nested get.

Example 1 — Top-Level Key (Official Docs)

Read a weight when it exists; get null when it does not.

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

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

@debug map.get($font-weights, "medium"); // 500
@debug map.get($font-weights, "extra-bold"); // null

.weights {
  --medium: #{map.get($font-weights, "medium")};
  --missing: #{meta.inspect(map.get($font-weights, "extra-bold"))};
}

How It Works

"medium" resolves to 500. "extra-bold" is not in the map, so Sass returns null instead of failing the compile.

Example 2 — Nested Path (Official Docs)

Walk into Helvetica weights and read one leaf—or get null for a missing branch.

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

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

@debug map.get($fonts, "Helvetica", "weights", "regular"); // 400
@debug map.get($fonts, "Helvetica", "colors"); // null

.fonts {
  --regular: #{map.get($fonts, "Helvetica", "weights", "regular")};
  --colors: #{meta.inspect(map.get($fonts, "Helvetica", "colors"))};
}

How It Works

Sass follows Helveticaweightsregular. Asking for colors under Helvetica fails the path and returns null.

📈 Practical Patterns

Theme tokens, null vs false, and defaults.

Example 3 — Theme Tokens into CSS Variables

Read nested colors and a top-level radius into custom properties.

styles.scss
@use "sass:map";

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

.theme {
  --primary: #{map.get($theme, "colors", "primary")};
  --radius: #{map.get($theme, "radius")};
  --has-primary: #{map.has-key($theme, "colors", "primary")};
}

How It Works

Nested get pulls primary; flat get pulls radius. has-key confirms the nested path exists before you rely on it.

Example 4 — null vs Stored false

Missing keys and explicit null both look like null from get—flags need care.

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

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

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

How It Works

enabled is present and false. label is present but null. ghost is missing—also null. Use map.has-key when you must tell those cases apart.

Example 5 — Default with or

Fall back to a safe value when a key is missing.

styles.scss
@use "sass:map";

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

$weight: map.get($font-weights, "extra-bold") or 400;

.fallback {
  --weight: #{$weight};
}

How It Works

map.get returns null for extra-bold, so or 400 supplies the default. Perfect for optional theme overrides.

🚀 Real-World Use Cases

  • Design tokens — read colors, spacing, and radii from a theme map into CSS.
  • Typography scales — pull a single weight from a nested font map (docs sample).
  • Component APIs — accept a config map and get only the options you need.
  • Safe defaultsmap.get(...) or $fallback for optional overrides.
  • Guarded mixins — pair with map.has-key before using a nested branch.

🧠 How Compilation Works

1

Write SCSS

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

Source
2

Resolve the path

Dart Sass looks up the key or walks nested maps to the last key.

Compile
3

Return a value

You get the stored value, or null if the path fails.

Result
4

CSS sees tokens

Only the colors, sizes, or numbers you interpolate remain.

⚠️ Common Pitfalls

  • Treating null as an error — missing keys are normal; add a default or check has-key.
  • Using or with boolean flags — a stored false is falsy, so a fallback may fire incorrectly.
  • Nested keys on old tooling — multi-key map.get needs Dart Sass 1.27+.
  • Expecting mutationget only reads; use map.set / merge helpers to change maps.
  • Forgetting quotes — string keys like "medium" must match how the map was written.

💡 Best Practices

✅ Do

  • Use @use "sass:map" and map.get()
  • Pass the full nested path when reading deep tokens
  • Pair with map.has-key for optional branches
  • Use or $fallback for missing optional values
  • Prefer Dart Sass 1.27+ for nested design-system maps

❌ Don’t

  • Expect the browser to evaluate map.get
  • Assume missing keys throw—they return null
  • Use or blindly when values may be false
  • Rely on nested multi-key get under LibSass
  • Confuse read (get) with write (set / merge)

Key Takeaways

Knowledge Unlocked

Five things to remember about map.get()

Read by key or path—missing returns null; nested needs Dart Sass 1.27+.

5
Core concepts
📦 02

Module

sass:map

@use
🔢 03

Missing

returns null

Rule
04

Nested

multi-key path

Safe
05

Legacy

map-get still works

Tooling

❓ Frequently Asked Questions

map.get($map, $key, $keys...) returns the value for a key. With one key it reads the top level. With more keys (Dart Sass 1.27+) it walks nested maps and returns the value at the last key.
It returns null. That is not an error. Use map.has-key() when you need a true/false check, or combine with or for a default: map.get($map, $key) or $default.
Yes. map-get($map, $key) is the older global name. Prefer map.get after @use "sass:map". Nested key paths with more than two arguments need Dart Sass 1.27+ and the module API.
map.get returns the stored value (or null). map.has-key returns true or false for whether the key exists—useful when the real value might be null or false.
Two-argument get works widely via map-get / map.get. Passing multiple keys for nested lookup is Dart Sass 1.27+ only. Prefer current Dart Sass for design-system maps.
No. It only reads. Use map.set, map.merge, or map.deep-merge when you need a new map with updates.
Did you know?

Official Sass docs highlight multi-key map.get because design systems often nest maps several levels deep. The same path style works with map.has-key, map.set, and the deep helpers like map.deep-merge.

Conclusion

map.get() is the everyday way to read Sass maps—flat or nested. Load it through sass:map, handle null with defaults or has-key, and use multi-key paths on Dart Sass 1.27+ for design-system trees.

Continue with map.has-key() or the Sass introduction.

Next: check if a key exists

Learn map.has-key() for optional tokens and @if guards.

map.has-key() →

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