Unlike languages like JavaScript, Sass uses words rather than symbols for its boolean operators. There is no &&, ||, or ! in Sass, instead you write and, or, and not.
not <expression>returns the opposite of the expression's value: it turnstrueintofalseandfalseintotrue.<expression> and <expression>returnstrueif both expressions' values aretrue, andfalseif either isfalse.<expression> or <expression>returnstrueif either expression's value istrue, andfalseif both arefalse.
Basic Examples
@debug not true; // false
@debug not false; // true
@debug true and true; // true
@debug true and false; // false
@debug true or false; // true
@debug false or false; // falseThe same code in the indented .sass syntax (no semicolons):
@debug not true // false
@debug not false // true
@debug true and true // true
@debug true and false // false
@debug true or false // true
@debug false or false // falseTruth Tables
not
| Expression | Result |
|---|---|
not true |
false |
not false |
true |
and
| Left | Right | Result |
|---|---|---|
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
or
| Left | Right | Result |
|---|---|---|
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
Truthiness and Falsiness
Anywhere true or false are allowed, you can use other values as well.
For example, if you want to check if a string contains a space, you can use string.index($string, " "). The string.index() function returns null if the substring isn't found and a number otherwise. So the result works directly as a boolean:
@use "sass:string";
$string: "hello world";
@if string.index($string, " ") {
// truthy: a number was returned, so the string contains a space
@debug "Has a space";
} @else {
// falsy: null was returned
@debug "No space";
}Heads up!
This differs from JavaScript, where 0, "", and [] are falsy. In Sass:
@debug if(0, "truthy", "falsy"); // truthy
@debug if("", "truthy", "falsy"); // truthy
@debug if((), "truthy", "falsy"); // truthy (empty list)
@debug if(null, "truthy", "falsy"); // falsy
@debug if(false, "truthy", "falsy"); // falsyOperator Precedence
Sass has a standard order of operations. From tightest to loosest, the relevant operators are:
- The unary operators
not,+,-, and/(tightest) *,/, and%+and->,>=,<, and<===and!=andor=(loosest, only in function arguments)
This means not binds tighter than and, which binds tighter than or. Comparison operators (==, !=, <, >, …) bind tighter than both and and or.
@debug 1 + 2 * 3 == 1 + (2 * 3); // true
@debug true or false and false == true or (false and false); // trueIn the second line, and binds tighter than or, so false and false is grouped first, then the ors are evaluated left to right.
Use parentheses for clarity
You can explicitly control the order of operations using parentheses. An operation inside parentheses is always evaluated before any operations outside it, and parentheses can be nested.
// Without parentheses: "and" binds tighter than "or"
@debug true or false and false; // true (== true or (false and false))
// With parentheses: force "or" to be evaluated first
@debug (true or false) and false; // false (== (true or false) and false)When combining several operators, add parentheses to make the intent obvious, even when they aren't strictly required.
Using Boolean Operators in Conditionals
Boolean operators are most often used inside @if, @else if, and while expressions to combine conditions.
Example: @if with and
@mixin button($large, $disabled) {
@if $large and not $disabled {
font-size: 1.5rem;
padding: 1rem 2rem;
} @else if $large and $disabled {
font-size: 1.5rem;
padding: 1rem 2rem;
opacity: 0.5;
} @else {
font-size: 1rem;
padding: 0.5rem 1rem;
}
}
.btn-primary {
@include button($large: true, $disabled: false);
}Example: or with relational operators
$columns: 4;
@if $columns < 1 or $columns > 12 {
@error "Column count must be between 1 and 12.";
}Example: combining all three
@mixin theme($dark, $compact) {
@if $dark and not $compact {
background: #333;
color: #fff;
padding: 2rem;
} @else if $dark or $compact {
background: #222;
color: #eee;
padding: 1rem;
} @else {
background: #fff;
color: #000;
padding: 1rem;
}
}Where Booleans Come From
Boolean values aren't only written as the literals true and false. They are also returned by:
- Equality operators:
==and!= - Relational operators:
<,<=,>,>= - Built-in functions, such as
math.comparable()andmap.has-key()
@use "sass:math";
@use "sass:map";
$breakpoints: ("sm": 576px, "md": 768px);
@debug 1px == 2px; // false
@debug 1px == 1px; // true
@debug 10px < 3px; // false
@debug math.comparable(100px, 3in); // true
@debug map.has-key($breakpoints, "md"); // trueSummary
| Operator | Syntax | Returns true when… |
|---|---|---|
not |
not <expr> |
the expression is false/null |
and |
<expr> and <expr> |
both expressions are truthy |
or |
<expr> or <expr> |
at least one expression is truthy |
Key rules to remember:
- Use words, not symbols:
and/or/not, never&&/||/!. - Only
falseandnullare falsy.0,"", and()are truthy. - Precedence is
not>and>or; comparison operators bind tighter still. - Use parentheses to make complex conditions unambiguous.