Sass meta.global-variable-exists() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:meta · globals only

What You’ll Learn

meta.global-variable-exists() belongs to the built-in sass:meta module. It answers: is there a global variable named X? This page covers the no-$ name rule, locals vs globals, $module, vs meta.variable-exists, and five compiled examples.

01

Concept

Is this global defined?

02

Module

@use "sass:meta"

03

Returns

true / false

04

Name

Without $

05

vs locals

variable-exists

06

Practice

5 examples

What Is meta.global-variable-exists()?

Official docs: returns whether a global variable named $name exists (without the $). Use it for optional theme tokens, safe defaults, and library APIs that only care about root-level configuration.

  • Without $module, checks for an un-namespaced global in the current file’s context.
  • With $module, checks a variable inside a @use namespace.
  • Variables declared inside a selector / mixin / function are local—this helper returns false for them.
💡
Beginner tip

Think of a company directory: global-variable-exists only lists HQ numbers. Desk phones in individual offices (locals) need meta.variable-exists instead.

📝 Syntax

styles.scss
@use "sass:meta";

// Recommended
meta.global-variable-exists($name, $module: null)

// Legacy global name
global-variable-exists($name, $module: null)

Parameters

ParameterTypeRequiredDescription
$nameStringYesVariable name without $. Example: "brand" checks $brand.
$moduleString or nullNoIf set, must match a @use namespace in the current file.

Return value

TypeResult
Booleantrue if that global exists; otherwise false

📦 Loading sass:meta

styles.scss
// Recommended — namespaced
@use "sass:meta";
$ok: meta.global-variable-exists("brand");

// Optional — bring members into scope
@use "sass:meta" as *;
$ok: global-variable-exists("brand");

// Legacy global
$ok: global-variable-exists("brand");

🔗 Global vs Local Variables

Official sample: a root $var1 is global; a $var2 inside h1 { … } is local. Only the global is found by this helper.

Where declaredglobal-variable-existsvariable-exists (same spot)
File root / global scopetruetrue
Inside a rule / mixin / functionfalsetrue (in that scope)
Never declaredfalsefalse

📦 Using the $module Argument

After @use "./tokens" as tokens, check that module’s globals with $module: "tokens". The string must match the namespace used in this file.

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

// Built-in modules expose members; $e is a known math constant in sass:math
$has-e: meta.global-variable-exists("e", $module: "math");

🛠 Compatibility

This is about Sass compilers, not browsers. The check runs at compile time.

Implementationglobal-variable-exists
Dart SassYes — prefer meta.global-variable-exists with modules
LibSassYes (legacy global / limited modules)
Ruby SassYes (legacy global / limited modules)

Prefer current Dart Sass so @use + $module work as documented.

⚡ Quick Reference

GoalCode
Import meta@use "sass:meta";
Check a globalmeta.global-variable-exists("brand")
Inside a modulemeta.global-variable-exists("gap", $module: "tokens")
Optional branchif(meta.global-variable-exists("brand"), $brand, $fallback)
Include locals toometa.variable-exists("brand")
Avoidmeta.global-variable-exists("$brand") (do not pass $)

Examples Gallery

Each example uses meta.global-variable-exists at compile time. Open View Compiled CSS for verified Dart Sass output.

📚 Getting Started

Official-style before/after checks and local vs global.

Example 1 — Before and After Defining a Global

A name is missing until you declare it at the root.

styles.scss
@use "sass:meta";

.before {
  --var1: #{meta.global-variable-exists("var1")};
}

$var1: value;

.after {
  --var1: #{meta.global-variable-exists("var1")};
}

How It Works

Matches the official docs idea: "var1" is false until $var1 is defined globally, then true.

Example 2 — Locals Are Not Global

A variable inside a rule does not count as a global.

styles.scss
@use "sass:meta";

$brand: #0ea5e9;

.card {
  $accent: #f59e0b; // local
  --brand-global: #{meta.global-variable-exists("brand")};
  --accent-global: #{meta.global-variable-exists("accent")};
}

How It Works

$brand lives at the root—true. $accent is local to .cardfalse for global-variable-exists.

📈 Practical Patterns

Optional defaults, module checks, and vs variable-exists.

Example 3 — Optional Theme Branch

Pick a configured global when present; otherwise use a fallback value.

styles.scss
@use "sass:meta";

// App / theme provided this global
$brand: #2563eb;

.btn {
  background: if(
    meta.global-variable-exists("brand"),
    $brand,
    #64748b
  );
}

.chip {
  background: if(
    meta.global-variable-exists("accent"),
    #f59e0b,
    #94a3b8
  );
}

How It Works

$brand exists globally, so .btn uses it. accent was never defined, so .chip takes the slate fallback. For simple overrides, $brand: #2563eb !default; is often enough without an exists check.

Example 4 — Check a Variable in a Module

Use $module for a @use namespace such as sass:math.

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

.probe {
  --math-e: #{meta.global-variable-exists("e", $module: "math")};
  --math-missing: #{meta.global-variable-exists("nope", $module: "math")};
  --bare-e: #{meta.global-variable-exists("e")};
}

How It Works

math.$e exists on the math namespace. A bare "e" check (no module) does not see that namespaced member.

Example 5 — Compare with variable-exists

Same local name: global check fails, scoped check succeeds.

styles.scss
@use "sass:meta";

.panel {
  $gap: 1rem; // local
  --global-gap: #{meta.global-variable-exists("gap")};
  --any-gap: #{meta.variable-exists("gap")};
}

How It Works

Official docs point you to meta.variable-exists when locals matter. Here both checks run in the same scope so the difference is obvious.

🚀 Real-World Use Cases

  • Optional theme tokens — apply fallbacks only when a global is missing.
  • Library configuration — detect whether the consumer set root options.
  • Module-aware tools — query another file’s globals via $module.
  • Migration helpers — preferred modern check vs deprecated feature-exists.
  • Debug / assert — fail early if a required global was never defined.

🧠 How Compilation Works

1

Pass a bare name

Call meta.global-variable-exists("brand") (no $).

Source
2

Sass searches globals

Looks in global scope or the named @use module.

Compile
3

Boolean drives @if

Your stylesheet keeps or skips optional configuration paths.

Branch
4

CSS ships

Browsers never see the check—only finished values.

⚠️ Common Pitfalls

  • Including $ in the name — pass "brand", not "$brand".
  • Expecting locals — use meta.variable-exists for scoped variables.
  • Forgetting $module — namespaced members will not match a bare check.
  • Wrong namespace string — must match this file’s @use … as name.
  • Expecting runtime checks — everything resolves when Sass compiles.

💡 Best Practices

✅ Do

  • Use @use "sass:meta" and the namespaced form
  • Pass bare names without $
  • Use this for root configuration / theme tokens
  • Prefer !default when a simple override API is enough
  • Reach for variable-exists when locals matter

❌ Don’t

  • Pass "$name" with a dollar sign
  • Assume it finds variables inside selectors
  • Use it for browser @supports questions
  • Forget $module for @use namespaces
  • Expect the browser to evaluate the check

Key Takeaways

Knowledge Unlocked

Five things to remember about meta.global-variable-exists()

Ask whether a global variable exists—name without $.

5
Core concepts
📦 02

Module

sass:meta

@use
03

Returns

boolean

Result
🔒 04

Scope

globals only

Rule
05

Name

no $ prefix

Syntax

❓ 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.
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.
Did you know?

Official Sass docs pair this helper with meta.variable-exists on purpose: the same local $var2 is false for global-variable-exists and true for variable-exists when checked inside that rule.

Conclusion

meta.global-variable-exists() returns whether a named global variable exists. Pass the name without $, use $module for @use namespaces, and switch to meta.variable-exists when you also need locals.

Continue with meta.inspect() or the Sass introduction.

Next: inspect Sass values

Learn meta.inspect() to stringify values while debugging.

meta.inspect() →

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