Bird
Raised Fist0
SASSmarkup~5 mins

Variable scope (global vs local) in SASS - Quick Revision & Key Differences

Choose your learning style10 modes available
Recall & Review
beginner
What is a global variable in Sass?
A global variable in Sass is a variable declared outside any selector or function. It can be accessed anywhere in the stylesheet unless overridden by a local variable with the same name.
Click to reveal answer
beginner
What is a local variable in Sass?
A local variable in Sass is a variable declared inside a selector, function, or mixin. It only exists within that block and cannot be accessed outside it.
Click to reveal answer
intermediate
How do you make a local variable affect the global variable in Sass?
You use the !global flag when assigning a variable inside a block. This updates the global variable instead of creating a new local one.
Click to reveal answer
intermediate
What happens if you declare a variable with the same name both globally and locally in Sass?
The local variable overrides the global one inside its block. Outside the block, the global variable value is used.
Click to reveal answer
beginner
Why is understanding variable scope important in Sass?
It helps avoid unexpected styles by controlling where variables can be used or changed. It keeps stylesheets organized and easier to maintain.
Click to reveal answer
Where is a Sass variable declared outside any selector accessible?
AAnywhere in the stylesheet
BOnly inside the selector it is declared
COnly inside functions
DNowhere, it is invalid
What does the !global flag do in Sass?
ACreates a new local variable
BMakes a local variable update the global variable
CDeletes the global variable
DPrevents variable usage
If a variable is declared both globally and locally with the same name, which one is used inside the local block?
AGlobal variable
BNeither variable
CBoth variables
DLocal variable
Can a local variable in Sass be accessed outside its block?
AYes, always
BOnly if declared with !global
CNo, never
DOnly inside functions
Why should you be careful with variable scope in Sass?
ATo avoid unexpected style changes
BTo make CSS slower
CTo increase file size
DTo disable variables
Explain the difference between global and local variables in Sass and how the !global flag works.
Think about where variables can be used and changed.
You got /4 concepts.
    Describe a real-life example where variable scope in Sass helps keep your styles organized.
    Imagine managing colors or sizes in a big project.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which statement about Sass variable scope is true?
      $color: red; declared outside any block means:
      easy
      A. The variable cannot be used unless declared inside a selector.
      B. The variable is local and only works inside the block it is declared in.
      C. The variable is global and can be used anywhere in the stylesheet.
      D. The variable must be declared with !local to be used globally.

      Solution

      1. Step 1: Understand where the variable is declared

        The variable $color: red; is declared outside any selector or block, making it global.
      2. Step 2: Recall Sass variable scope rules

        Global variables can be accessed anywhere in the stylesheet, inside or outside blocks.
      3. Final Answer:

        The variable is global and can be used anywhere in the stylesheet. -> Option C
      4. Quick Check:

        Global variable outside blocks = accessible everywhere [OK]
      Hint: Variables outside blocks are global by default [OK]
      Common Mistakes:
      • Thinking variables outside blocks are local
      • Confusing !global with !local
      • Assuming variables need special flags to be global
      2. Which Sass syntax correctly updates a global variable $font-size inside a nested block?
      easy
      A. Inside block: $font-size: 2rem;
      B. Outside block: $font-size: 2rem !local;
      C. Inside block: !global $font-size: 2rem;
      D. Inside block: $font-size: 2rem !global;

      Solution

      1. Step 1: Identify how to update global variables inside blocks

        To update a global variable inside a nested block, you must add !global after the value.
      2. Step 2: Check each option's syntax

        Inside block: $font-size: 2rem !global; uses $font-size: 2rem !global; which is correct Sass syntax for updating global variables inside blocks.
      3. Final Answer:

        Inside block: $font-size: 2rem !global; -> Option D
      4. Quick Check:

        Use !global to update global variables inside blocks [OK]
      Hint: Add !global after value to update global inside blocks [OK]
      Common Mistakes:
      • Forgetting !global and only assigning value locally
      • Using !local which is invalid
      • Placing !global before variable name
      3. What color will the p text be in this Sass code?
      $color: blue;
      
      p {
        $color: red;
        color: $color;
      }
      medium
      A. Blue
      B. Red
      C. Undefined (error)
      D. Inherits from body

      Solution

      1. Step 1: Identify variable scopes

        $color is set globally to blue, but inside p block, a local $color is set to red.
      2. Step 2: Determine which $color is used in color: $color;

        Inside the block, the local $color (red) overrides the global one.
      3. Final Answer:

        Red -> Option B
      4. Quick Check:

        Local variable overrides global inside blocks [OK]
      Hint: Local variables override global inside blocks [OK]
      Common Mistakes:
      • Assuming global variable is used inside block
      • Thinking Sass throws error for local variable
      • Confusing inheritance with variable scope
      4. Find the error in this Sass code that tries to update a global variable inside a nested block:
      $padding: 10px;
      
      .container {
        $padding: 20px;
        padding: $padding;
      }
      medium
      A. Missing !global to update the global variable inside the block.
      B. Variable $padding cannot be used inside blocks.
      C. Global variables cannot be declared outside blocks.
      D. The syntax for variable declaration is incorrect.

      Solution

      1. Step 1: Analyze variable update inside block

        The code declares $padding: 20px; inside .container block without !global, so it creates a local variable instead of updating the global one.
      2. Step 2: Identify correct way to update global variable

        To update the global $padding inside the block, !global must be added: $padding: 20px !global;.
      3. Final Answer:

        Missing !global to update the global variable inside the block. -> Option A
      4. Quick Check:

        Use !global to update global variables inside blocks [OK]
      Hint: Add !global to update global variables inside blocks [OK]
      Common Mistakes:
      • Assuming local assignment updates global variable
      • Thinking variables can't be used inside blocks
      • Confusing variable declaration syntax
      5. Given this Sass code, what will be the final value of $margin after compilation?
      $margin: 5px;
      
      .wrapper {
        $margin: 10px !global;
        .content {
          $margin: 15px;
        }
      }
      
      body {
        margin: $margin;
      }
      hard
      A. 10px
      B. 5px
      C. 15px
      D. Compilation error

      Solution

      1. Step 1: Track global variable changes

        Initially, $margin is 5px globally. Inside .wrapper, $margin: 10px !global; updates the global variable to 10px.
      2. Step 2: Check nested block .content

        Inside .content, $margin: 15px; is local to that block and does not affect the global variable.
      3. Step 3: Determine value used in body

        The body selector uses the global $margin, which was updated to 10px.
      4. Final Answer:

        10px -> Option A
      5. Quick Check:

        !global updates global variable; local inside nested blocks doesn't [OK]
      Hint: !global updates global; nested local variables don't affect global [OK]
      Common Mistakes:
      • Assuming nested local variable changes global
      • Confusing variable values inside nested blocks
      • Expecting compilation error due to variable shadowing
      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