0
0
SASSmarkup~15 mins

String types and concatenation in SASS - Deep Dive

Choose your learning style9 modes available
Overview - String types and concatenation
What is it?
In Sass, strings are sequences of characters used to represent text. They can be written with quotes (single or double) or without quotes in some cases. Concatenation means joining two or more strings together to form one longer string.
Why it matters
Strings let you create dynamic styles by combining text values like colors, fonts, or URLs. Without string handling and concatenation, you would have to write repetitive code and lose flexibility in styling. This makes your stylesheets harder to maintain and update.
Where it fits
Before learning string types and concatenation, you should understand basic Sass syntax and variables. After this, you can explore string functions, interpolation, and advanced text manipulation in Sass.
Mental Model
Core Idea
Strings in Sass are text pieces that you can join together to build new text values for styling.
Think of it like...
Think of strings like beads on a necklace. Each bead is a piece of text, and concatenation is like threading beads together to make a longer necklace.
Strings: "hello" + " world" → "hello world"

+-------------------+
| "hello"          |
+-------------------+
          +
+-------------------+
| " world"         |
+-------------------+
          ↓
+-----------------------+
| "hello world"         |
+-----------------------+
Build-Up - 7 Steps
1
FoundationUnderstanding Sass string basics
🤔
Concept: Learn what strings are in Sass and how to write them.
Strings in Sass can be written with single quotes ('text'), double quotes ("text"), or sometimes without quotes if they are simple words. For example: $greeting: 'Hello'; $word: "World"; $plain: hello; All three are strings but behave slightly differently when output.
Result
You can store text in variables using quotes or no quotes, and Sass recognizes them as strings.
Knowing how to write strings correctly helps you avoid errors and understand how Sass treats text values.
2
FoundationWhat is string concatenation?
🤔
Concept: Concatenation means joining strings to make a longer string.
In Sass, you can join strings by placing them next to each other inside interpolation or using the + operator in newer Sass versions. For example: $full: #{$greeting} #{$word}; This joins 'Hello' and 'World' with a space between.
Result
The result is a single string 'Hello World' created by joining two strings.
Understanding concatenation lets you build complex text values dynamically instead of writing them all out manually.
3
IntermediateUsing interpolation for concatenation
🤔Before reading on: do you think you can join strings simply by writing them side by side without any special syntax? Commit to your answer.
Concept: Interpolation #{ } lets you insert variables or expressions inside strings to join them.
You cannot just write $greeting $word; to join strings. Instead, use interpolation: $full: "#{$greeting} #{$word}"; This tells Sass to replace the variables with their values and join them inside the string.
Result
The output is the combined string 'Hello World' inside quotes.
Knowing interpolation is key because it is the main way Sass combines strings and variables in styles.
4
IntermediateConcatenation with the + operator
🤔Before reading on: do you think the + operator in Sass works exactly like in JavaScript for strings? Commit to your answer.
Concept: Sass supports + to join strings, but it behaves differently than JavaScript.
You can write: $full: $greeting + " " + $word; This joins the strings with a space. But if you use + with numbers or other types, Sass tries to do math instead.
Result
The result is the string 'Hello World' joined by + operators.
Understanding that + can mean addition or concatenation depending on context helps avoid bugs.
5
IntermediateQuoted vs unquoted string behavior
🤔Before reading on: do you think quoted and unquoted strings behave the same in all Sass operations? Commit to your answer.
Concept: Quoted strings keep their quotes in output; unquoted strings do not.
For example: $quoted: 'Hello'; $unquoted: Hello; When output, $quoted prints with quotes, $unquoted prints without. This affects concatenation and CSS output.
Result
You see differences in how strings appear in the final CSS depending on quotes.
Knowing this difference helps you control exactly how text appears in your stylesheets.
6
AdvancedConcatenation pitfalls with spaces and quotes
🤔Before reading on: do you think spaces between strings in concatenation always appear in the output? Commit to your answer.
Concept: Spaces and quotes affect how concatenated strings appear and can cause unexpected results.
If you write: $full: #{$greeting}#{$word}; The result is 'HelloWorld' without space. To add space, you must include it explicitly: $full: "#{$greeting} #{$word}"; Also, mixing quoted and unquoted strings can add or remove quotes unexpectedly.
Result
You get either 'HelloWorld' or 'Hello World' depending on spaces and quotes.
Understanding how spaces and quotes behave prevents subtle bugs in your CSS output.
7
ExpertHow Sass compiles string concatenation internally
🤔Before reading on: do you think Sass concatenates strings at runtime in the browser? Commit to your answer.
Concept: Sass processes string concatenation during compilation, not in the browser.
When you write concatenation in Sass, the compiler merges strings into one before generating CSS. This means the browser only sees the final string, not the parts or variables. This compilation step optimizes performance and ensures consistent output.
Result
The browser receives plain CSS with combined strings, no Sass variables or operations remain.
Knowing compilation timing helps you understand why Sass variables and concatenation don't exist in the final CSS and how to debug output.
Under the Hood
Sass treats strings as data types during compilation. When concatenation is requested, it merges string values in memory before writing the CSS file. Interpolation triggers Sass to evaluate variables and expressions inside strings, replacing them with their values. The compiler distinguishes between quoted and unquoted strings to decide whether to keep quotes in output. The + operator is overloaded to perform addition or concatenation based on operand types.
Why designed this way?
Sass was designed to extend CSS with programming features while producing clean CSS output. Handling strings at compile time ensures no runtime overhead in browsers. Differentiating quoted and unquoted strings preserves CSS syntax correctness. Overloading + allows familiar syntax but requires careful type handling to avoid confusion.
+-------------------+      +-------------------+
|   String A        |  +   |   String B        |
+-------------------+      +-------------------+
           ↓                      ↓
       Sass Compiler concatenates strings
                   ↓
          +-------------------+
          |  Combined String   |
          +-------------------+
                   ↓
          Generates final CSS file
Myth Busters - 4 Common Misconceptions
Quick: do you think unquoted strings always behave exactly like quoted strings in Sass? Commit to yes or no.
Common Belief:Unquoted strings and quoted strings are the same in Sass and can be used interchangeably.
Tap to reveal reality
Reality:Unquoted strings lose their quotes in output and can behave differently in concatenation and CSS generation compared to quoted strings.
Why it matters:Using unquoted strings when quotes are needed can break CSS syntax or cause unexpected styles.
Quick: do you think the + operator always concatenates strings in Sass like in JavaScript? Commit to yes or no.
Common Belief:The + operator in Sass always joins strings just like in JavaScript.
Tap to reveal reality
Reality:In Sass, + can mean addition or concatenation depending on operand types, which can cause errors if you mix numbers and strings.
Why it matters:Misusing + can lead to math operations instead of string joining, causing wrong CSS values.
Quick: do you think spaces between concatenated strings appear automatically in the output? Commit to yes or no.
Common Belief:When concatenating strings, spaces between them are added automatically in the output.
Tap to reveal reality
Reality:Spaces must be explicitly included; concatenation joins strings exactly as given without adding spaces.
Why it matters:Forgetting to add spaces leads to merged words or invalid CSS values.
Quick: do you think Sass concatenates strings at runtime in the browser? Commit to yes or no.
Common Belief:Sass concatenates strings dynamically in the browser when CSS loads.
Tap to reveal reality
Reality:Sass concatenates strings during compilation, so the browser only sees the final combined string.
Why it matters:Expecting runtime concatenation can confuse debugging and performance expectations.
Expert Zone
1
Sass treats quoted and unquoted strings differently in functions like url() and content, affecting CSS output subtly.
2
Interpolation can force unquoted strings to behave like quoted ones, which is useful for controlling output precisely.
3
The + operator's dual role requires careful type awareness to avoid silent bugs in complex expressions.
When NOT to use
Avoid using + for concatenation when mixing numbers and strings; prefer interpolation for clarity. For complex string manipulations, use Sass string functions or consider preprocessing outside Sass. When precise CSS syntax is critical, always use quoted strings.
Production Patterns
In real projects, developers use interpolation to build dynamic URLs, font names, or class names. They carefully manage quotes to ensure valid CSS. Concatenation is often combined with variables and functions to create reusable, maintainable stylesheets.
Connections
Template literals in JavaScript
Both use interpolation to embed variables inside strings.
Understanding Sass interpolation helps grasp how JavaScript template literals build dynamic strings, showing a shared pattern across languages.
String concatenation in shell scripting
Both join text pieces but differ in syntax and quoting rules.
Knowing how shell scripts handle strings clarifies why Sass distinguishes quoted and unquoted strings and requires explicit concatenation.
Human language sentence building
Concatenation is like combining words to form sentences with spaces and punctuation.
Recognizing string concatenation as sentence building helps understand the importance of spaces and quotes for clear, correct output.
Common Pitfalls
#1Forgetting to add spaces between concatenated strings.
Wrong approach:$full: #{$greeting}#{$word};
Correct approach:$full: "#{$greeting} #{$word}";
Root cause:Assuming concatenation automatically adds spaces, leading to merged words.
#2Using + operator with mixed types causing math instead of concatenation.
Wrong approach:$result: $number + "px";
Correct approach:$result: "#{$number}px";
Root cause:Not realizing + triggers addition when operands are numbers, breaking string joining.
#3Mixing quoted and unquoted strings without understanding output differences.
Wrong approach:$url: url('images/' + $filename);
Correct approach:$url: url("images/#{$filename}");
Root cause:Confusing how quotes affect CSS syntax and output formatting.
Key Takeaways
Strings in Sass represent text and can be quoted or unquoted, affecting how they appear in CSS.
Concatenation joins strings to build new text values, but spaces and quotes must be managed explicitly.
Interpolation #{ } is the main way to combine strings and variables dynamically in Sass.
The + operator can mean addition or concatenation depending on context, so use it carefully.
Sass processes all string operations during compilation, producing clean CSS without runtime overhead.