Sass Boolean Operators

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
not · and · or

What You’ll Learn

Sass boolean operators use words—not symbols—for logic: not, and, and or. This page covers truthiness, how null behaves, combining conditions in @if, and five compiled examples.

01

Operators

not and or

02

Returns

true / false

03

Falsey

false & null

04

Truthy

0, "", lists

05

Use

@if guards

06

Practice

5 examples

What Are Boolean Operators?

Official docs: unlike languages like JavaScript, Sass uses words rather than symbols for its boolean operators.

  • not flips a value: truefalse.
  • and needs both sides truthy.
  • or needs at least one side truthy.
  • Logic runs when Sass compiles—not in the browser.
  • Most often used inside @if, mixins, and helpers.
💡
Beginner tip

Read conditions out loud: “if dark and not compact” becomes @if $dark and not $compact. Sass chooses the branch before CSS is written.

📝 Syntax

styles.scss
not $expression

$expression-a and $expression-b

$expression-a or $expression-b

Operators

OperatorNameReturns
notnegationOpposite of the expression’s value
andconjunctiontrue if both sides are truthy; otherwise false
ordisjunctiontrue if either side is truthy; otherwise false

Official samples

ExpressionResult
not truefalse
not falsetrue
true and truetrue
true and falsefalse
true or falsetrue
false or falsefalse

vs JavaScript symbols

IdeaJavaScriptSass
Negate!not
Both true&&and
Either true||or

⚖️ Truthiness and Falsiness

Official docs: anywhere true or false are allowed, you can use other values as well.

  • Falsey — only false and null.
  • Truthy — every other value, including 0, "", and empty lists.
ValueIn a condition
trueTruthy
falseFalsey
nullFalsey
0Truthy
"" (empty string)Truthy
() (empty list)Truthy
⚠️
Heads up

Official docs: some languages treat more values as falsey. Sass is not one of them. Empty strings, empty lists, and 0 are all truthy.

🔎 Null Checks & string.index

Official docs: to check if a string contains a space, write string.index($string, " "). The function returns null if the substring is missing and a number otherwise—so it works as a condition directly.

styles.scss
@use "sass:string";

$string: "Hello World";

@if string.index($string, " ") {
  // Found a space (number is truthy)
} @else {
  // Missing → null is falsey
}

🔁 Mixing With Comparisons

Boolean operators often wrap equality or relational checks:

styles.scss
@if $width >= 600px and $theme == "dark" {
  // wide + dark
}

@if not ($mode == "print") {
  // everything except print
}

@if $flag or $fallback {
  // use a default when $flag is false/null
}

Parentheses help when you negate a whole comparison: not ($mode == "print").

⚡ Quick Reference

GoalCode
Negatenot $flag
Both required$a and $b
Either allowed$a or $b
Branch@if $dark and not $compact { ... }
Substring exists@if string.index($s, " ") { ... }
Explicit null test$value != null
Inspect a booleanmeta.inspect($a and $b)

Examples Gallery

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

📚 Getting Started

Official not, and, and or samples.

Example 1 — Basic Boolean Ops

Flip values and combine plain true / false.

styles.scss
@use "sass:meta";

.ops {
  --not-t: #{meta.inspect(not true)};
  --not-f: #{meta.inspect(not false)};
  --and-tt: #{meta.inspect(true and true)};
  --and-tf: #{meta.inspect(true and false)};
  --or-tf: #{meta.inspect(true or false)};
  --or-ff: #{meta.inspect(false or false)};
}

How It Works

Matches the official docs exactly. Remember: Sass writes and/or/not, not &&/||/!.

Example 2 — Truthiness Surprises

Prove that 0, "", and empty lists are truthy.

styles.scss
@use "sass:meta";

.ops {
  --zero: #{meta.inspect(if(0, true, false))};
  --empty-str: #{meta.inspect(if("", true, false))};
  --empty-list: #{meta.inspect(if((), true, false))};
  --nullish: #{meta.inspect(if(null, true, false))};
  --falsey: #{meta.inspect(if(false, true, false))};
}

How It Works

The built-in if() function picks a branch from a condition. Only null and false take the false path here.

📈 Practical Patterns

Substring checks, theme guards, and fallback flags.

Example 3 — string.index as a Condition

Official pattern: a found index is truthy; missing returns null.

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

$label: "Hello World";
$plain: "HelloWorld";

.has-space {
  --ok: #{meta.inspect(string.index($label, " ") != null)};
  --no: #{meta.inspect(string.index($plain, " ") != null)};
  --raw-ok: #{meta.inspect(string.index($label, " "))};
  --raw-no: #{meta.inspect(string.index($plain, " "))};
}

How It Works

Index 6 means the space was found (1-based). You can also write @if string.index($label, " ") without != null because numbers are truthy.

Example 4 — @if With and / not

Emit styles only when multiple flags line up.

styles.scss
$dark: true;
$compact: false;

.card {
  @if $dark and not $compact {
    --mode: dark-roomy;
    background: #111;
    padding: 2rem;
  } @else {
    --mode: other;
  }
}

How It Works

Both sides of and pass, so only the dark-roomy branch is emitted. The @else styles never appear in CSS.

Example 5 — Fallback With or

Treat null like “missing” and fall back to another flag.

styles.scss
@use "sass:meta";

$flag: null;
$fallback: true;

.gate {
  --use-fallback: #{meta.inspect($flag or $fallback)};
  --both: #{meta.inspect($fallback and not $flag)};
}

How It Works

null is falsey, so $flag or $fallback succeeds via the fallback. not $flag is true because null is falsey.

🚀 Real-World Use Cases

  • Feature flags — emit optional CSS only when a flag is on.
  • Theme combos$dark and not $high-contrast.
  • Optional arguments — treat null as “not provided”.
  • String probes — use string.index results as conditions.
  • Guard rails — skip mixins when a required token is missing.

🧠 How Compilation Works

1

Write a condition

Combine flags with not, and, or or.

Source
2

Apply truthiness

Only false and null count as falsey.

Compile
3

Choose a branch

Keep the matching @if / @else styles.

Emit
4

CSS ships

Browsers only see finished CSS like background: #111.

⚠️ Common Pitfalls

  • JS habits!, &&, and || are not Sass boolean ops.
  • Assuming 0 is falsey — in Sass it is truthy.
  • Empty string / list — also truthy.
  • Forgetting parentheses — prefer not ($a == $b) for clarity.
  • Expecting runtime logic — these operators never appear in CSS.

💡 Best Practices

✅ Do

  • Use word operators: not, and, or
  • Treat only false and null as falsey
  • Combine with == / >= for clear guards
  • Use string.index results as conditions when helpful
  • Parenthesize negated comparisons

❌ Don’t

  • Copy JavaScript ! / && / || into Sass
  • Assume empty values are falsey
  • Confuse null with false for equality (null != false)
  • Over-nest conditions when a helper variable would be clearer
  • Expect browsers to evaluate Sass boolean logic

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass boolean operators

Word-based logic at compile time, with a small falsey set.

5
Core concepts
📦 02

Result

true / false

Output
03

Falsey

false & null

Rule
04

Truthy

0, "", lists

Surprise
05

Use

@if guards

Pattern

❓ Frequently Asked Questions

Unlike JavaScript symbols (!, &&, ||), Sass uses words: not, and, and or. They combine or invert boolean values at compile time.
not returns the opposite of an expression’s value: not true is false, and not false is true. With truthiness, not null is true.
and returns true when both sides are truthy, and false when either side is falsey.
or returns true when either side is truthy, and false when both sides are falsey.
Only false and null. Empty strings, empty lists, and the number 0 are all truthy in Sass—unlike some other languages.
Official docs: use string.index($string, " "). It returns null when missing (falsey) and a number when found (truthy), so it works directly in @if.
Did you know?

Official Sass docs highlight that empty strings, empty lists, and 0 are truthy—a common surprise for developers coming from JavaScript.

Conclusion

Sass boolean operators not, and, and or give you clear compile-time logic. Remember the tiny falsey set (false and null), then drive @if branches with confidence.

Continue with Sass @use or browse equality operators.

Next: Sass @use Rule

Load modules with namespaces, aliases, and with configuration.

@use rule →

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