meta.variable-exists() belongs to the built-in sass:meta module. It answers: is there a variable named X in the current scope? This page covers the no-$ name rule, locals vs globals, vs meta.global-variable-exists, and five compiled examples.
01
Concept
Is this name in scope?
02
Module
@use "sass:meta"
03
Returns
true / false
04
Name
Without the $
05
vs global
Locals count here
06
Practice
5 examples
Concept
What Is meta.variable-exists()?
Official docs: returns whether a variable named $name (without the $) exists in the current scope. That includes globals you can see from here and locals declared in the current rule, mixin, or function.
Pass the bare name: "var1" checks $var1.
Missing names return false—they do not throw.
There is no$module argument (unlike global-variable-exists).
See also meta.global-variable-exists() when you only want file-level globals.
💡
Beginner tip
Think of looking around the room you are in: variable-exists finds the desk lamp (local) and the hallway light you can still see (global). global-variable-exists only asks about the hallway.
Foundation
📝 Syntax
styles.scss
@use "sass:meta";
// Recommended
meta.variable-exists($name)
// Legacy global name
variable-exists($name)
Parameters
Parameter
Type
Required
Description
$name
String
Yes
Variable name to look up without the leading $.
Return value
Type
Result
Boolean
true if the variable exists in the current scope; otherwise false
Modules
📦 Loading sass:meta
styles.scss
// Recommended — namespaced
@use "sass:meta";
$ok: meta.variable-exists("brand");
// Optional — bring members into scope
@use "sass:meta" as *;
$ok: variable-exists("brand");
// Legacy global
$ok: variable-exists("brand");
Details
🏢 Locals vs Globals
Official sample: a root $var1 is global; a $var2 inside h1 { … } is local. variable-exists finds both when you ask from a scope that can see them.
Where defined
variable-exists
global-variable-exists
File root (global)
true (from scopes that see it)
true
Inside a rule / mixin / function (local)
true (in that scope)
false
Never defined
false
false
Compare
📋 Related Meta Helpers
Helper
Checks
Returns
meta.variable-exists
Current scope (local + global)
Boolean
meta.global-variable-exists
Globals only (+ optional $module)
Boolean
meta.module-variables
All variables in a @use module
Map of values
meta.function-exists
Functions
Boolean
meta.mixin-exists
Mixins
Boolean
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The lookup runs at compile time.
Implementation
variable-exists
Dart Sass
Yes — prefer meta.variable-exists
LibSass
Yes (legacy global)
Ruby Sass
Yes (legacy global)
Prefer current Dart Sass and the namespaced meta.* API.
$label lives in the mixin scope and is seen from .chip. ghost was never defined.
Applications
🚀 Real-World Use Cases
Optional theme hooks — use $brand only when the caller defined it.
Local overrides — detect a rule-level $accent without requiring a global.
Safer if() defaults — avoid reading names that might not exist.
Mixin internals — branch on locals created inside the mixin.
Migration aid — pair with global-variable-exists to understand scope bugs.
🧠 How Compilation Works
1
Pass a bare name
Call meta.variable-exists("brand") (no $).
Source
2
Sass searches this scope
Looks at locals and reachable globals from the current location.
Compile
3
Boolean drives if() / @if
Your stylesheet keeps or skips optional variable paths.
Branch
4
✓
CSS ships
Browsers never see meta.variable-exists—only finished values.
Watch Out
⚠️ Common Pitfalls
Including $ in the name — check "brand", not "$brand".
Wrong helper for globals-only — use global-variable-exists when locals must not count.
Expecting $module — this function has no module argument.
Reading the variable before the guard — use if() so the missing branch never evaluates $missing.
Expecting runtime checks — everything resolves when Sass compiles.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:meta" and meta.variable-exists()
Pass names without $
Use this when locals should count
Pair with lazy if() for optional values
Prefer Dart Sass + the namespaced API
❌ Don’t
Pass "$name" with a dollar sign
Assume it only finds globals
Expect a $module parameter
Reference a missing variable outside a guarded if()
Expect the browser to evaluate the check
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about meta.variable-exists()
Ask whether a variable name exists in the current scope—locals included.
5
Core concepts
📝01
API
variable-exists($name)
Call
📦02
Module
sass:meta
@use
✅03
Returns
boolean
Result
🔗04
Name
without $
Detail
✓05
Scope
locals + globals
Key
❓ Frequently Asked Questions
meta.variable-exists($name) returns true if a variable named $name exists in the current scope, otherwise false. Pass the name without the $.
No. Official docs use the bare name: meta.variable-exists("var1") checks $var1.
variable-exists finds locals and globals in the current scope. global-variable-exists only finds globals—a variable declared inside a rule is local, so global-variable-exists is false while variable-exists is true.
No. Unlike global-variable-exists, variable-exists only takes $name and checks the current scope.
Yes. variable-exists($name) is the legacy global name. Prefer meta.variable-exists after @use "sass:meta".
No. It runs at compile time. Browsers only see the CSS after your @if / if() branches resolve.
Did you know?
Official Sass docs point from variable-exists to global-variable-exists on purpose: one question is “can I see this name here?” and the other is “is it a true global?”—easy to mix up when debugging scope.
meta.variable-exists() returns whether a named variable exists in the current scope. Pass the name without $, remember locals count, and switch to meta.global-variable-exists when you only want file-level globals.