Sass Equality Operators

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
== & != · compile time

What You’ll Learn

Sass equality operators answer “are these two values the same?” Use == for equal and != for not equal. This page covers numbers (including units), strings, colors, lists, maps, booleans, and five compiled examples.

01

Operators

== / !=

02

Returns

true / false

03

Numbers

Value + units

04

Strings

Quote-insensitive

05

Lists

Separator matters

06

Practice

5 examples

What Are Equality Operators?

Official docs: equality operators return whether or not two values are the same. They are written == (equal) and != (not equal). Two values are equal if they are the same type and the same value—and “same value” depends on the type.

  • Results are booleans: true or false.
  • Most often used inside @if, guards, and helpers.
  • Comparison happens when Sass compiles—not in the browser.
  • Pair with relational operators (>, <, …) for ordering numbers.
💡
Beginner tip

Think of a checklist: “Is this theme dark?” becomes $mode == "dark". Sass answers true or false before CSS is written.

📝 Syntax

styles.scss
// Equal — true when both sides match
$expression-a == $expression-b

// Not equal — true when the sides differ
$expression-a != $expression-b

Operators

OperatorNameReturns true when…
==EqualBoth sides are the same type and same value
!=not equalThe sides differ in type or value

Return value

TypeResult
Booleantrue or false

🎨 How Equality Works By Type

TypeEqual when…
NumbersSame value and units, or equal after unit conversion
StringsSame contents—even if one is quoted and one is not
ColorsSame color space and channels, or same legacy RGBA values
ListsSame contents, same separator, and same bracket style
MapsSame keys and values
true / false / nullOnly equal to themselves
CalculationsSame name and arguments (operations compared textually)
FunctionsSame function reference (same definition site)

⚖️ Numbers, Units & Unitless Values

ExpressionResultWhy
1px == 1pxtrueSame value and unit
1px != 1emtrueIncompatible units
1 != 1pxtrueUnitless is not equal to with-units (Dart Sass)
96px == 1intrueCompatible units convert
⚠️
Unitless equality (compatibility)

Official docs: LibSass and older Ruby Sass treated unitless numbers as equal to the same number with any unit. That behavior violated transitivity and was removed from modern releases. Prefer Dart Sass, where 1 != 1px.

🛠 Compatibility

This is about Sass compilers, not browsers. Equality is resolved at compile time.

Implementation== / !=Unitless vs with units
Dart SassYesNot equal (modern rule)
LibSassYesOld behavior: unitless could equal with-units
Ruby SassYesOld versions matched LibSass; fixed later

Prefer current Dart Sass so unit rules match the official docs.

⚡ Quick Reference

GoalCode
Equal$a == $b
not equal$a != $b
Branch on a mode@if $mode == "dark" { ... }
Quoted vs unquoted string"Helvetica" == Helveticatrue
Compatible units96px == 1intrue
Inspect a booleanmeta.inspect($a == $b)

Examples Gallery

Each example uses == / != at compile time. Open View Compiled CSS for verified output (booleans shown with meta.inspect).

📚 Getting Started

Official number and string equality samples.

Example 1 — Number Equality

Same unit, different units, unitless vs with units, and conversion.

styles.scss
@use "sass:meta";

.probe {
  --same: #{meta.inspect(1px == 1px)};
  --diff-unit: #{meta.inspect(1px != 1em)};
  --unitless: #{meta.inspect(1 != 1px)};
  --convert: #{meta.inspect(96px == 1in)};
}

How It Works

Matches the official docs: compatible units can convert (96px == 1in), while unitless 1 is not equal to 1px in Dart Sass.

Example 2 — String Equality

Quoted and unquoted contents can still match.

styles.scss
@use "sass:meta";

.probe {
  --q-u: #{meta.inspect("Helvetica" == Helvetica)};
  --diff: #{meta.inspect("Helvetica" != "Arial")};
}

How It Works

Official docs call strings unusual: quote markers do not break equality when the text is the same.

📈 Practical Patterns

Colors, list shape rules, maps, and @if branches.

Example 3 — Color Equality

HSL can equal a hex when channels match; alpha differences fail equality.

styles.scss
@use "sass:meta";

.probe {
  --hsl-hex: #{meta.inspect(hsl(34, 35%, 92.1%) == #f2ece4)};
  --alpha: #{meta.inspect(rgba(179, 115, 153, 0.5) != rgba(179, 115, 153, 0.8))};
}

How It Works

Legacy color spaces can compare by RGBA channel values, so equivalent colors written differently may still be equal.

Example 4 — List Equality

Contents, separator, and brackets all matter.

styles.scss
@use "sass:meta";

.probe {
  --same: #{meta.inspect((5px 7px 10px) == (5px 7px 10px))};
  --vals: #{meta.inspect((5px 7px 10px) != (10px 14px 20px))};
  --sep: #{meta.inspect((5px 7px 10px) != (5px, 7px, 10px))};
  --brack: #{meta.inspect((5px 7px 10px) != [5px 7px 10px])};
}

How It Works

Space vs comma separators are different lists. Brackets also change equality, even when the numbers look identical.

Example 5 — Maps, Booleans & @if

Compare maps, use equality in a branch, and remember null != false.

styles.scss
@use "sass:meta";

$theme: ("venus": #998099, "nebula": #d2e1dd);
$mode: "dark";

.card {
  @if $mode == "dark" {
    --ok: #{meta.inspect(true)};
    background: #111;
  } @else {
    background: #fff;
  }
  --map-eq: #{meta.inspect($theme == ("venus": #998099, "nebula": #d2e1dd))};
  --bool: #{meta.inspect(true != false)};
  --nullish: #{meta.inspect(null != false)};
}

How It Works

The @if branch keeps only the dark styles. Maps compare key/value pairs. null is not the same as false.

🚀 Real-World Use Cases

  • Theme switches@if $theme == "dark" for alternate palettes.
  • Feature flags — emit rules only when a flag is true.
  • Token guards — reject unexpected string or number options.
  • Color checks — confirm two tokens resolve to the same color.
  • List shape checks — ensure a helper received a space- or comma-separated list.

🧠 How Compilation Works

1

Write a comparison

Use == or != between two expressions.

Source
2

Sass compares by type rules

Applies number, string, color, list, map, and boolean rules.

Compile
3

Drive a branch or value

Feed the boolean into @if or store it for debugging.

Emit
4

CSS ships

Browsers only see finished CSS like background: #111.

⚠️ Common Pitfalls

  • Unitless vs with units — in Dart Sass, 1 != 1px.
  • List separators — space lists are not equal to comma lists.
  • Brackets[1 2] is not equal to (1 2).
  • null vs false — they are not equal.
  • Expecting browser runtime== never appears in CSS.

💡 Best Practices

✅ Do

  • Use == / != inside @if for clear branches
  • Remember quoted and unquoted strings can match
  • Account for unit conversion on numbers
  • Compare list shape (separator + brackets) intentionally
  • Prefer modern Dart Sass for unitless rules

❌ Don’t

  • Assume unitless equals with-units on Dart Sass
  • Ignore separators when comparing lists
  • Treat null as false
  • Expect browsers to evaluate equality
  • Rely on LibSass unitless quirks

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass equality

Compare values at compile time with == and !=.

5
Core concepts
📦 02

Result

true / false

Output
🔗 03

Numbers

units & conversion

Rule
📚 04

Strings

quotes optional

Rule
05

Use

@if branches

Pattern

❓ Frequently Asked Questions

Sass provides == (equal) and != (not equal). They return true or false at compile time by comparing two values.
When they have the same value and the same units, or when their values match after converting between compatible units. Example: 96px == 1in is true. Unitless 1 is not equal to 1px in modern Dart Sass.
Yes, if the contents match. Official docs: "Helvetica" == Helvetica is true.
Yes. Space-separated lists are not equal to comma-separated lists, and bracketed lists are not equal to unbracketed lists, even with the same items.
No. Official docs: null != false is true. true, false, and null are only equal to themselves.
No. == and != are resolved when Sass compiles. The CSS file only keeps the resulting true/false branch or emitted values.
Did you know?

Official Sass docs show 96px == 1in as true—equality can convert compatible units, while still keeping unitless numbers distinct from unit values in Dart Sass.

Conclusion

Sass equality operators == and != compare values at compile time. Learn each type’s rules—especially units, string quotes, and list separators—then drive @if branches with clear boolean results.

Continue with relational operators or browse meta.inspect().

Next: Sass Relational Operators

Order numbers with <, <=, >, and >=.

Relational operators →

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