Sass meta.global-variable-exists()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass

Definition and Usage

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

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.

Global vs Local

Same name, different helpers:

Where declaredglobal-variable-existsvariable-exists
File root / global scopetruetrue
Inside a rule / mixin / functionfalsetrue (in that scope)
Never declaredfalsefalse
In a @use moduleUse $module: "…"N/A (no $module)
vs other member checks.

Prefer meta.function-exists, meta.mixin-exists, and this helper over deprecated meta.feature-exists.

SCSS Examples

Examples use @use "sass:meta". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Before and After Defining a Global

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

SCSS
@use "sass:meta";

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

$var1: value;

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

How It Works

  1. Bare name "var1" looks for $var1.
  2. Before the declaration, the check is false.
  3. Root assignment creates a global.
  4. Afterward, the same check is true.

Example 2 — Locals Are Not Global

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

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

  1. $brand lives at the root — true.
  2. $accent is local to .card.
  3. global-variable-exists ignores that local.
  4. Matches the official docs local/global distinction.

Example 3 — Optional Theme Branch

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

SCSS
@use "sass:meta";

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

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

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

How It Works

  1. $brand exists globally, so .btn uses it.
  2. accent was never defined.
  3. .chip takes the slate fallback.
  4. Tip: for simple overrides, $brand: … !default; is often enough.

Example 4 — Check a Variable in a Module

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

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

  1. math.$e exists on the math namespace.
  2. $module: "math" finds it.
  3. Missing names in that module return false.
  4. Bare "e" (no module) does not see the namespaced member.

Example 5 — Compare with variable-exists

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

SCSS
@use "sass:meta";

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

How It Works

  1. $gap is local to .panel.
  2. global-variable-exists returns false.
  3. variable-exists returns true in that scope.
  4. Use both when you need to teach the difference.

Edge Cases & Pitfalls

Most mistakes are including $, expecting locals, or forgetting $module.

name

No dollar sign in the string

Pass "brand", not "$brand".

scope

Locals stay invisible

Use meta.variable-exists for variables inside rules.

module

Namespaced members need $module

Match the @use namespace string, or pass a module value.

compile

Compile-time only

Browsers never see the check—only the finished CSS values.

Sass Support

Prefer the module form with current Dart Sass.

APIStatus
meta.global-variable-exists()Dart Sass via @use "sass:meta"
Return typeBoolean
Name argumentString without $
Legacy globalglobal-variable-exists($name, $module: null)

Compile with the Dart Sass CLI.

Key Takeaways

  • 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).

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.

Next: Sass meta.inspect()

Get a debug string for any Sass value.

meta.inspect() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful