meta.global-variable-exists($name, $module: null) returns a boolean: whether a global variable named $name exists. Pass the name without the $. Load it with @use "sass:meta".
Official sample idea.
meta.global-variable-exists("var1") is false until you declare $var1 at the root; a $var2 inside a rule stays local and still returns false.
Pass $module for namespaces.
After @use "sass:math", check module members with $module: "math". The value can also be a module value.
Globals only.
Variables declared inside a selector, mixin, or function are local. Use meta.variable-exists when the current scope matters.
Syntax
Syntax
SCSS
@use "sass:meta";
meta.global-variable-exists($name, $module: null); //=> boolean
meta.global-variable-exists("var1"); // false until $var1 is defined
$var1: value;
meta.global-variable-exists("var1"); // true
// Global alias (legacy): global-variable-exists($name, $module: null)
// Prefer meta.global-variable-exists after @use "sass:meta"
Pattern: pass a bare name (and optional module) → get true/false → branch with @if for optional config.
Returns true/false for a global variable by bare name.
Locals need variable-exists; this helper ignores them.
Use $module to check variables under a @use namespace.
Prefer @use "sass:meta" over the legacy global name.
One line: to ask whether a root-level variable is defined, use meta.global-variable-exists($name, $module: null).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:meta"
2. Pass bare name without $
3. Optional $module for @use namespaces
4. Branch with @if for optional config
5. Use variable-exists when locals matter
Frequently Asked Questions
meta.global-variable-exists($name, $module: null) returns true if a global variable named $name exists, otherwise false. Pass the name without the $.
No. Official docs use the bare name: meta.global-variable-exists("var1") checks $var1.
global-variable-exists only finds globals. variable-exists returns true for locals in the current scope as well. A variable declared inside a rule is local—global-variable-exists is false, variable-exists is true.
When checking a variable from a @use namespace. $module must match that namespace string in the current file, or you can pass a module value.
Yes. global-variable-exists($name, $module: null) is the legacy global name. Prefer meta.global-variable-exists after @use "sass:meta".
No. It runs at compile time. Browsers only see the CSS after your @if branches resolve.