Home Sass Booleans Sass Booleans Definition
Definition and Usage Booleans are the logical values true and false. They come from literals, equality and relational operators, and many built-in functions.
Combine with words. Use and, or, and not—not &&, ||, or !.
Drive styles with @if. When a condition is truthy, Sass emits that block at compile time. Browsers never see the boolean logic.
Only false and null are falsey. Empty strings, empty lists, and 0 are all truthy in Sass—unlike JavaScript.
Syntax
Syntax $circle: true;
@if $circle {
border-radius: 50%;
}
// Operators (words, not && / || / !)
true and false // false
true or false // true
not false // true
// Comparisons return booleans
1px == 1px // true
10px < 3px // false Pattern: get a boolean → combine with and / or / not → branch with @if (or pass as a mixin flag).
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Compare 2. Operators 3. @if 4. Truthy 5. index Example 1 — Official Comparisons Return Booleans Equality, relational ops, and math.compatible() produce true / false.
@use "sass:math";
.demo {
--eq-false: #{1px == 2px};
--eq-true: #{1px == 1px};
--lt-false: #{10px < 3px};
--compat: #{math.compatible(100px, 3in)};
} .demo {
--eq-false: false;
--eq-true: true;
--lt-false: false;
--compat: true;
} How It Works == / < evaluate at compile time.Results are boolean values.math.compatible also returns a boolean.Matches the official comparisons sample idea.Example 2 — Official and / or / not Word operators combine or invert booleans.
.demo {
--and-t: #{true and true};
--and-f: #{true and false};
--or-t: #{true or false};
--or-f: #{false or false};
--not-t: #{not true};
--not-f: #{not false};
} .demo {
--and-t: true;
--and-f: false;
--or-t: true;
--or-f: false;
--not-t: false;
--not-f: true;
} How It Works and needs both sides true.or needs either side true.not flips a single value.Details on boolean operators .Example 3 — Official @if Mixin Flag Pass a boolean into a mixin to optionally emit styles.
@use "sass:math";
@mixin avatar($size, $circle: false) {
width: $size;
height: $size;
@if $circle {
border-radius: math.div($size, 2);
}
}
.square-av {
@include avatar(100px, $circle: false);
}
.circle-av {
@include avatar(100px, $circle: true);
} .square-av {
width: 100px;
height: 100px;
}
.circle-av {
width: 100px;
height: 100px;
border-radius: 50px;
} How It Works $circle: false skips border-radius.$circle: true emits the radius.math.div computes half the size.Matches the official Sass docs sample.Example 4 — Truthiness and Falsiness Only false and null fail conditions; 0, "", and () pass.
@mixin check($name, $cond) {
.#{$name} {
@if $cond {
content: "truthy";
} @else {
content: "falsey";
}
}
}
@include check(false-lit, false);
@include check(null-lit, null);
@include check(zero, 0);
@include check(empty-str, "");
@include check(empty-list, ()); .false-lit {
content: "falsey";
}
.null-lit {
content: "falsey";
}
.zero {
content: "truthy";
}
.empty-str {
content: "truthy";
}
.empty-list {
content: "truthy";
} How It Works false / null are falsey.0 is truthy (unlike JS)."" and () are also truthy.More on null .Example 5 — string.index as a Condition Functions that return null or a number work directly in @if.
@use "sass:string";
@mixin check($name, $cond) {
.#{$name} {
@if $cond {
content: "truthy";
} @else {
content: "falsey";
}
}
}
@include check(space, string.index("hello world", " "));
@include check(missing, string.index("hello", " ")); .space {
content: "truthy";
}
.missing {
content: "falsey";
} How It Works Found → returns a number (truthy).Missing → returns null (falsey).No extra != null check required.Details on string.index() . Edge Cases & Pitfalls Most boolean bugs come from JS-style operators or JS-style falsiness.
0 0 is truthy Do not treat zero like JavaScript. Compare explicitly if you need “is zero”.
"" Empty string / list are truthy Check length or equality when emptiness matters.
@if Compile-time only @if never runs in the browser. Output is plain CSS.
Toolchain
Sass Support Booleans and boolean operators are core Sass on Dart Sass.
Feature Status true / falseSupported and / or / notSupported @if / @elseSupported Truthy / falsey rules Only false and null are falsey
Compile with the Dart Sass CLI .
Remember
Key Takeaways true / false are the boolean values.
and / or / not combine them (word operators).
@if emits styles when the condition is truthy.
Only false and null are falsey.
One line: use booleans (and truthy checks) with @if to choose what CSS Sass emits.
Practice
Compile Notes Practical checklist:
1. Use true / false literals or comparison results
2. Combine with and / or / not (not && / || / !)
3. Branch styles with @if / @else
4. Remember only false and null are falsey
5. Pass boolean flags into mixins for optional CSS Frequently Asked Questions What are Sass booleans? Booleans are the logical values true and false. They come from literals, equality/relational operators, and many built-in functions such as map.has-key().
How do I combine booleans? Use word operators: and (both true), or (either true), and not (opposite). Unlike JavaScript, Sass does not use &&, ||, or !.
What values are falsey in Sass? Only false and null. Empty strings, empty lists, and the number 0 are all truthy.
How do I use a boolean in styles? Use @if $flag { … } to include a block when the condition is truthy.
Does @if run in the browser? No. Boolean logic and @if run at compile time. Browsers only see the CSS that was emitted.
How is string.index useful with booleans? string.index returns null when a substring is missing (falsey) and a number when found (truthy), so it works directly in @if conditions.
Next: Sass null Learn the other falsey value and how missing results use null.
null → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion