Bird
Raised Fist0
SASSmarkup~15 mins

Boolean values and logic in SASS - Deep Dive

Choose your learning style10 modes available
Overview - Boolean values and logic
What is it?
Boolean values in Sass are simple true or false values used to control how styles are applied. Logic means using these true or false values to make decisions in your styles, like choosing colors or showing elements only when certain conditions are met. This helps make your styles smarter and more flexible. Without Boolean logic, styles would be static and repetitive.
Why it matters
Boolean values and logic let you write styles that adapt to different situations, like changing a button color when hovered or showing a menu only on mobile. Without this, you would have to write many repetitive styles manually, making your code long and hard to maintain. Boolean logic saves time and makes your styles easier to update and understand.
Where it fits
Before learning Boolean values and logic, you should know basic Sass syntax like variables and nesting. After this, you can learn about control directives like @if, @else, and loops to create even more dynamic styles.
Mental Model
Core Idea
Boolean values are like simple yes/no answers that help Sass decide which styles to apply.
Think of it like...
Imagine a light switch that can be either ON or OFF. Boolean values are like that switch, telling Sass whether to turn a style ON (true) or OFF (false).
Boolean Logic in Sass

  +-----------+      +-----------+
  | Condition | ---> | true/false|
  +-----------+      +-----------+
         |                 |
         v                 v
  +-----------------------------+
  | Apply styles if true, skip  |
  +-----------------------------+
Build-Up - 6 Steps
1
FoundationUnderstanding Boolean Values
🤔
Concept: Introduce what Boolean values are in Sass and their two possible states: true and false.
In Sass, Boolean values are simple true or false values. They are used to represent conditions. For example, you can set a variable $is-dark-mode: true; to say dark mode is on. These values help Sass decide which styles to apply.
Result
You can store and use true or false in variables to represent conditions.
Understanding that Boolean values are just true or false helps you think about style decisions as simple yes/no questions.
2
FoundationUsing Boolean Values in Conditions
🤔
Concept: Show how to use Boolean values in Sass @if statements to control styles.
@if statements check if a condition is true or false. For example: $is-dark-mode: true; body { @if $is-dark-mode { background: black; color: white; } @else { background: white; color: black; } } This applies dark or light styles based on the Boolean value.
Result
Styles change depending on whether $is-dark-mode is true or false.
Knowing how to use Boolean values in @if lets you create styles that change automatically based on conditions.
3
IntermediateBoolean Logic Operators in Sass
🤔Before reading on: do you think 'and' requires both conditions true or just one? Commit to your answer.
Concept: Introduce logical operators like and, or, and not to combine or invert Boolean values.
Sass supports logical operators: - and: true if both conditions are true - or: true if at least one condition is true - not: reverses the Boolean value Example: $is-mobile: true; $is-logged-in: false; @if $is-mobile and $is-logged-in { // styles here apply only if both are true } @if not $is-logged-in { // styles apply if user is NOT logged in }
Result
You can combine multiple conditions to make complex decisions in styles.
Understanding logical operators lets you build more precise style rules that depend on multiple factors.
4
IntermediateBoolean Values in Functions and Mixins
🤔Before reading on: do you think you can pass Boolean values as arguments to mixins? Commit to yes or no.
Concept: Show how Boolean values can be passed to functions and mixins to control their behavior.
You can pass Boolean values to mixins or functions to decide what styles they generate. @mixin button($is-primary) { background: if($is-primary, blue, gray); color: white; } @include button(true); // blue button @include button(false); // gray button
Result
Mixins generate different styles based on Boolean arguments.
Knowing that Booleans can be passed around lets you write reusable style blocks that adapt to different needs.
5
AdvancedShort-Circuit Evaluation in Sass Logic
🤔Before reading on: do you think Sass evaluates all parts of a logical expression even if the result is already known? Commit to yes or no.
Concept: Explain how Sass stops evaluating conditions early when the result is already determined (short-circuiting).
In expressions like $a and $b, if $a is false, Sass does not check $b because the whole expression cannot be true. This is called short-circuit evaluation. Example: @if false and (some-function()) { // some-function() is NOT called } This improves performance and avoids errors from unnecessary checks.
Result
Sass skips unnecessary checks in logical expressions, making styles faster and safer.
Understanding short-circuiting helps you write efficient conditions and avoid side effects in your style logic.
6
ExpertBoolean Logic with Sass Maps and Complex Conditions
🤔Before reading on: can you use Boolean logic directly inside map functions or must you extract values first? Commit to your answer.
Concept: Show how Boolean logic can be combined with Sass maps and functions for advanced style decisions.
You can store Boolean values inside maps and use logic to pick styles dynamically. $themes: ( dark: true, light: false ); @mixin theme-styles($theme) { @if map-get($themes, $theme) { background: black; color: white; } @else { background: white; color: black; } } @include theme-styles(dark); // dark styles @include theme-styles(light); // light styles
Result
Styles adapt dynamically based on Boolean values stored in complex data structures.
Knowing how to combine Boolean logic with maps unlocks powerful, scalable style systems.
Under the Hood
Sass evaluates Boolean values as simple true or false flags during compilation. When it encounters a condition, it checks the Boolean value and decides which styles to include in the final CSS. Logical operators combine these Booleans using standard logic rules, and short-circuit evaluation stops unnecessary checks early. This all happens before the browser sees the CSS, so the output is clean and efficient.
Why designed this way?
Boolean logic was designed to let developers write flexible styles without repeating code. Early CSS was static and repetitive, so Sass introduced Booleans and logic to automate decisions. The design balances simplicity (true/false) with power (operators and conditions) to keep styles readable and maintainable.
Sass Boolean Logic Flow

+------------------+
| Sass Compiler    |
+------------------+
          |
          v
+------------------+      +------------------+
| Evaluate Boolean | ---> | true or false    |
| expressions      |      +------------------+
+------------------+
          |
          v
+------------------+
| Apply styles if  |
| condition true   |
+------------------+
Myth Busters - 4 Common Misconceptions
Quick: Do you think the string 'false' is treated as false in Sass conditions? Commit to yes or no.
Common Belief:Many believe that the string 'false' counts as false in Sass conditions.
Tap to reveal reality
Reality:In Sass, only the actual Boolean false is false; the string 'false' is treated as true because it is a non-empty string.
Why it matters:This causes unexpected styles to apply if you use strings instead of Boolean values, leading to bugs that are hard to spot.
Quick: Do you think 0 is false in Sass Boolean logic? Commit to yes or no.
Common Belief:Some think that 0 is false in Sass conditions, like in some programming languages.
Tap to reveal reality
Reality:In Sass, 0 is treated as true because it is a number, not a Boolean false.
Why it matters:This can cause conditions to behave differently than expected, especially for developers coming from other languages.
Quick: Do you think Sass evaluates all parts of a logical expression even if the first condition is false? Commit to yes or no.
Common Belief:People often believe Sass always evaluates every part of a logical expression.
Tap to reveal reality
Reality:Sass uses short-circuit evaluation and stops checking once the result is known.
Why it matters:Knowing this prevents writing code that relies on side effects in later conditions, which might not run.
Quick: Do you think you can use JavaScript-style truthy/falsy values in Sass? Commit to yes or no.
Common Belief:Some assume Sass treats values like empty strings or null as false, like JavaScript.
Tap to reveal reality
Reality:Sass only treats the Boolean false as false; everything else is true, including empty strings and null.
Why it matters:This difference can cause logic errors when porting code or writing conditions.
Expert Zone
1
Sass treats only the Boolean false as false; all other values, including null and empty strings, are true in conditions.
2
Short-circuit evaluation in Sass can prevent errors by skipping function calls or expressions that would fail if evaluated unnecessarily.
3
Passing Boolean values to mixins and functions allows creating highly reusable and configurable style components without duplicating code.
When NOT to use
Boolean logic in Sass is not suitable for runtime decisions because Sass compiles styles before the browser runs. For dynamic runtime changes, use JavaScript or CSS custom properties instead.
Production Patterns
In production, Boolean logic is often combined with theme maps and configuration variables to build scalable design systems. Developers use Booleans to toggle features like dark mode, responsive layouts, or accessibility styles efficiently.
Connections
Programming Boolean Logic
Boolean logic in Sass is the same fundamental concept used in programming languages to control flow and decisions.
Understanding Boolean logic in programming helps grasp how Sass uses true/false to decide which styles to apply.
Digital Electronics
Boolean logic in Sass mirrors the logic gates in electronics that control circuits based on true/false signals.
Knowing how logic gates work in electronics can deepen understanding of how Sass combines conditions to produce final styles.
Decision Making in Everyday Life
Boolean logic is like making simple yes/no decisions in daily choices, such as 'If it is raining AND I have an umbrella, then go outside.'
Recognizing Boolean logic in everyday decisions helps internalize how Sass uses true/false to choose styles.
Common Pitfalls
#1Using strings instead of Boolean values in conditions.
Wrong approach:$is-active: 'false'; @if $is-active { color: green; } @else { color: red; }
Correct approach:$is-active: false; @if $is-active { color: green; } @else { color: red; }
Root cause:Confusing the string 'false' with the Boolean false causes Sass to treat the condition as true.
#2Assuming 0 is false in Sass conditions.
Wrong approach:$count: 0; @if $count { display: block; } @else { display: none; }
Correct approach:$count: 0; @if $count == 0 { display: none; } @else { display: block; }
Root cause:Misunderstanding that Sass treats 0 as true, so explicit comparison is needed.
#3Relying on all parts of a logical expression to run.
Wrong approach:@if false and some-function() { // code }
Correct approach:@if false and some-function() { // code }
Root cause:Expecting some-function() to run even when the first condition is false, ignoring short-circuit evaluation.
Key Takeaways
Boolean values in Sass are simple true or false flags that control which styles get applied.
Only the Boolean false counts as false; everything else, including strings and numbers, is true.
Logical operators like and, or, and not let you combine multiple conditions for precise style decisions.
Sass uses short-circuit evaluation to skip unnecessary checks, improving performance and safety.
Passing Boolean values to mixins and functions enables reusable, adaptable style components.

Practice

(1/5)
1. In Sass, which of the following represents a Boolean value?
easy
A. 1
B. "true"
C. "false"
D. true

Solution

  1. Step 1: Understand Sass Boolean values

    In Sass, Boolean values are the keywords true and false, not strings or numbers.
  2. Step 2: Identify the correct Boolean

    true uses true as a keyword, which is the correct Boolean value in Sass.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Boolean values in Sass are true or false [OK]
Hint: Look for keywords true or false without quotes [OK]
Common Mistakes:
  • Confusing strings "true" or "false" with Boolean values
  • Using numbers like 1 or 0 as Booleans
  • Thinking true/false must be quoted
2. Which of the following is the correct syntax to use a Boolean in an @if statement in Sass?
easy
A. @if $is-active == true { color: blue; }
B. @if $is-active = true { color: blue; }
C. @if $is-active === true { color: blue; }
D. @if $is-active != true { color: blue; }

Solution

  1. Step 1: Check Sass comparison operator

    Sass uses == to compare values in @if statements, not = or ===.
  2. Step 2: Identify correct syntax

    @if $is-active == true { color: blue; } uses == correctly to compare $is-active with true.
  3. Final Answer:

    @if $is-active == true { color: blue; } -> Option A
  4. Quick Check:

    Use double equals (==) for comparison in Sass [OK]
Hint: Use double equals (==) for comparisons in @if [OK]
Common Mistakes:
  • Using single equals (=) which assigns instead of compares
  • Using triple equals (===) which is invalid in Sass
  • Using != when checking for true
3. What color will the following Sass code output?
$dark-mode: false;

body {
  @if $dark-mode {
    background-color: black;
  } @else {
    background-color: white;
  }
}
medium
A. white
B. black
C. transparent
D. red

Solution

  1. Step 1: Check the Boolean value of $dark-mode

    $dark-mode is set to false, so the @if condition is false.
  2. Step 2: Follow the @else branch

    Since the condition is false, the code inside @else runs, setting background-color to white.
  3. Final Answer:

    white -> Option A
  4. Quick Check:

    False condition triggers @else block [OK]
Hint: False triggers @else styles, not @if [OK]
Common Mistakes:
  • Assuming false runs the @if block
  • Confusing true/false values
  • Ignoring the @else block
4. Identify the error in this Sass code:
$is-visible: true

.container {
  @if $is-visible == true {
    display: block;
  } @else {
    display: none;
  }
}
medium
A. Incorrect use of @else block
B. Wrong comparison operator in @if
C. Missing semicolon after variable declaration
D. Variable name cannot start with $

Solution

  1. Step 1: Check variable declaration syntax

    In Sass, variable declarations must end with a semicolon. Here, $is-visible: true is missing a semicolon.
  2. Step 2: Verify other syntax

    The comparison operator == and @else block are correct. Variable names can start with $.
  3. Final Answer:

    Missing semicolon after variable declaration -> Option C
  4. Quick Check:

    Variable declarations need semicolons [OK]
Hint: Check for missing semicolons after variables [OK]
Common Mistakes:
  • Forgetting semicolon after variable assignment
  • Confusing = and == in @if
  • Thinking @else is optional syntax
5. You want to apply a red border only if both $is-error and $is-active are true in Sass. Which code correctly uses Boolean logic to do this?
hard
A. @if $is-error & $is-active { border: 1px solid red; }
B. @if $is-error and $is-active { border: 1px solid red; }
C. @if $is-error && $is-active { border: 1px solid red; }
D. @if $is-error or $is-active { border: 1px solid red; }

Solution

  1. Step 1: Understand Sass Boolean operators

    Sass uses the keyword and for logical AND, not symbols like && or &.
  2. Step 2: Identify correct syntax

    @if $is-error and $is-active { border: 1px solid red; } correctly uses and to check if both variables are true before applying the border.
  3. Final Answer:

    @if $is-error and $is-active { border: 1px solid red; } -> Option B
  4. Quick Check:

    Use and for logical AND in Sass [OK]
Hint: Use 'and' keyword for AND logic in Sass [OK]
Common Mistakes:
  • Using && or & which are invalid in Sass
  • Using 'or' instead of 'and' for both true
  • Confusing bitwise & with logical and
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard