map.keys() belongs to the built-in sass:map module. It returns every key in a map as a comma-separated list. This page covers order, @each loops, nested maps, vs map.values, the legacy map-keys name, and five compiled examples.
01
Concept
List all keys
02
Module
@use "sass:map"
03
Returns
Comma-separated list
04
vs values
Names vs numbers
05
Loops
@each + get
06
Practice
5 examples
Concept
What Is map.keys()?
Maps store named pairs like "medium": 500. Sometimes you need the names—to loop, build class names, or count entries. Official docs: map.keys returns a comma-separated list of all keys in $map.
Order matches the map’s key order
Only top-level keys of the map you pass (not deep nested keys automatically)
Result is a list—use list.length, list.nth, or @each
💡
Beginner tip
Think of a table of contents: map.keys lists the chapter titles. map.values lists the page numbers. map.get opens one chapter.
Foundation
📝 Syntax
Load the map module, then call the function:
styles.scss
@use "sass:map";
map.keys($map)
// Legacy global name (still works)
map-keys($map)
Parameters
Parameter
Type
Required
Description
$map
Map
Yes
Map whose keys you want. Not modified.
Return value
Type
Result
List
Comma-separated list of all keys, in map order (empty list for an empty map)
Modules
📦 Loading sass:map
styles.scss
// Recommended — namespaced
@use "sass:map";
$names: map.keys($theme);
// Optional — bring members into scope
@use "sass:map" as *;
$names: keys($theme);
// Optional — custom namespace
@use "sass:map" as m;
$names: m.keys($theme);
// Legacy global
$names: map-keys($theme);
⚠️
Module vs legacy
Prefer map.keys with @use "sass:map". Keep map-keys for older snippets and LibSass-era code.
Compare
📋 map.keys vs map.values
Both return comma-separated lists in the same order. Pick the side of each pair you need.
map.keys
map.values
Returns
Key names
Stored values
Best for
Selectors, @each names, counting entries
Bulk math on numbers/colors
Typical follow-up
map.get($map, $key)
Use list helpers directly
Legacy name
map-keys
map-values
Details
🔍 Top-Level Only (Nested Maps)
map.keys($theme) does not flatten nested keys. If $theme has a "colors" map inside, you only see "colors" at the top level. To list color token names, pass the nested map:
Each key becomes part of a class name; map.get supplies the padding value. (You can also write @each $name, $size in $spacing—keys + get is clearer when you already think in map helpers.)
Same order, different sides of each pair. Index 1 in both lists refers to the same entry (regular / 400).
Applications
🚀 Real-World Use Cases
Utility generators — build .space-sm-style classes from a token map.
Theme introspection — list which top-level sections a config map defines.
Validation — compare map.keys against an allowed-keys list.
Docs / debug — print key names with meta.inspect while building systems.
Nested tokens — map.keys(map.get(...)) to list color or type scale names.
🧠 How Compilation Works
1
Write SCSS
Call map.keys($map) after @use "sass:map".
Source
2
Collect keys
Dart Sass builds a comma-separated list in map order.
Compile
3
Loop or inspect
Use @each, list.length, or pair with map.get.
Result
4
✓
CSS sees output
Only the rules or custom properties you emit remain.
Watch Out
⚠️ Common Pitfalls
Expecting nested keys — keys is top-level only; dig with map.get first.
Confusing keys with values — names vs stored data; use the matching helper.
Forgetting it is a list — use list module helpers, not map helpers, on the result.
Assuming quoted CSS output — interpolating a bare key may drop quotes; use meta.inspect when debugging.
Empty maps — you get (), not an error.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:map" and map.keys()
Loop with @each $key in map.keys($map)
Read values with map.get($map, $key) inside loops
Pass nested maps when you need child key names
Prefer Dart Sass for the modern module API
❌ Don’t
Expect the browser to evaluate map.keys
Assume one call flattens an entire nested tree
Call map functions on the returned list
Confuse keys with values or get
Rely only on legacy map-keys in new code
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about map.keys()
Comma-separated key list in map order—great for @each generators.
5
Core concepts
📝01
Call
map.keys($map)
API
📦02
Module
sass:map
@use
🔢03
Returns
comma-separated list
Rule
❓04
Scope
top-level keys only
Safe
✓05
Legacy
map-keys still works
Tooling
❓ Frequently Asked Questions
map.keys($map) returns a comma-separated list of all keys in the map, in the same order as the map. It does not return nested keys from child maps—only the top level of the map you pass.
map.keys returns the names (keys). map.values returns the stored values in the same order. Use keys when you need names for @each or selectors; use values when you only need the numbers/colors.
Yes. map-keys($map) is the older global name. Prefer map.keys after @use "sass:map".
No. map.keys only lists keys of the map you pass. To list keys inside a nested map, first get that nested map: map.keys(map.get($theme, "colors")).
map.keys(()) returns an empty list (). list.length of that list is 0.
Use @each $key in map.keys($map) { ... } and often map.get($map, $key) inside the loop to read each value.
Did you know?
You can also iterate a map directly with @each $key, $value in $map. map.keys shines when you only need names—or when you pass those names into other helpers and mixins.
map.keys() turns a map into a list of names—ideal for loops, generators, and introspection. Load it through sass:map, remember it is top-level only, and pair it with map.get or map.values as needed.