Sass meta.variable-exists()

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

Definition and Usage

meta.variable-exists($name) returns a boolean: whether a variable named $name exists in the current scope. Pass the name without the $. Load with @use "sass:meta".

Official sample idea.

meta.variable-exists("var1") is false until you declare $var1; a local $var2 inside a rule returns true in that scope.

Locals and globals.

Unlike meta.global-variable-exists, this helper sees variables declared in the current scope—including locals inside rules and mixins.

No $module argument.

It only takes $name. For namespaced module variables, use global-variable-exists with $module, or module-variables.

Syntax

SCSS
@use "sass:meta";

meta.variable-exists($name); //=> boolean

meta.variable-exists("var1"); // false until defined
$var1: value;
meta.variable-exists("var1"); // true

h1 {
  $var2: value; // local
  meta.variable-exists("var2"); // true here
}

// Global alias (legacy): variable-exists($name)
// Prefer meta.variable-exists after @use "sass:meta"

Pattern: pass a bare name → get true/false for the current scope → branch with @if.

Current Scope vs Global

Same name, different helpers:

Where declaredvariable-existsglobal-variable-exists
File root / global scopetruetrue
Inside a rule / mixin / functiontrue (in that scope)false
Never declaredfalsefalse
$module supportNoYes
Official contrast.

Docs show a local $var2 inside h1: variable-exists is true; global-variable-exists is false.

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 Variable

A name is missing until you define it (official var1 idea).

SCSS
@use "sass:meta";

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

$var1: value;

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

How It Works

  1. Bare name "var1" looks for $var1.
  2. Before the declaration, the check is false.
  3. Afterward, the same check is true.
  4. Matches the official Sass docs sample idea.

Example 2 — Locals Count in the Current Scope

A variable inside a rule is visible to variable-exists there.

SCSS
@use "sass:meta";

$brand: #0d9488;

.card {
  $accent: #f59e0b;
  --brand: #{meta.variable-exists("brand")};
  --accent: #{meta.variable-exists("accent")};
  --missing: #{meta.variable-exists("nope")};
}

How It Works

  1. $brand is global and visible here.
  2. $accent is local to .card.
  3. variable-exists finds both.
  4. nope was never defined → false.

Example 3 — vs global-variable-exists

Same names, different answers for a local variable.

SCSS
@use "sass:meta";

$brand: #0d9488;

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

How It Works

  1. Local $accent is found by variable-exists only.
  2. global-variable-exists returns false for it.
  3. Global $brand is found by both helpers.
  4. Matches the official local/global contrast.

Example 4 — Optional Values with @if

Use a variable when it exists; otherwise fall back.

SCSS
@use "sass:meta";

$brand: teal;

.box {
  @if meta.variable-exists("brand") {
    color: $brand;
  } @else {
    color: gray;
  }

  @if meta.variable-exists("accent") {
    border-color: $accent;
  } @else {
    border-color: silver;
  }
}

How It Works

  1. $brand exists → color is teal.
  2. $accent does not exist.
  3. @else supplies silver without reading a missing variable.
  4. @if only evaluates the taken branch.

Example 5 — Locals Inside a Mixin

A mixin-local variable is visible to nested rules in that mixin.

SCSS
@use "sass:meta";

@mixin chip {
  $label: "chip";
  .chip {
    --has-label: #{meta.variable-exists("label")};
    --has-ghost: #{meta.variable-exists("ghost")};
    content: $label;
  }
}

@include chip;

How It Works

  1. $label lives in the mixin scope.
  2. .chip can see that local.
  3. ghost was never defined.
  4. Scope-aware checks help optional mixin config.

Edge Cases & Pitfalls

Most variable-exists bugs are including $ or expecting module-wide checks.

name

No dollar sign in the string

Pass "brand", not "$brand".

scope

Current scope only

There is no $module argument—use global-variable-exists for namespaces.

vs

Locals need this helper

global-variable-exists ignores rule/mixin locals; this one finds them.

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.variable-exists()Dart Sass via @use "sass:meta"
Return typeBoolean
Name argumentString without $
Legacy globalvariable-exists($name)

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns true/false for a variable in the current scope.
  • Sees locals and globals; pass bare names without $.
  • No $module — use global-variable-exists for namespaces.
  • Prefer @use "sass:meta" over the legacy global name.

One line: to ask whether a variable exists where you are, use meta.variable-exists($name).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:meta"
2. Pass bare name without $
3. Check in the scope where you need the answer
4. Branch with @if for optional values
5. Use global-variable-exists for globals-only / $module

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 branches resolve.

Next: Sass selector.append()

Start the selector module—append one selector to another.

selector.append() →

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