JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Global vs. Local Scope
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 1-Grammar and Types

Scope

Scope refers to the area in which code is accessible within a program. This module will explain the differences between global and local scope for variables.

File Location

    javascript-library
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
                03-scope.js <-- You are here

Global vs. Local Scope

Global scope means that a piece of code is accessible throughout the entire program. This could be a variable, a code block, a function, or more. Local scope, on the other hand, means that a piece of code is only available within a limited part of the program. This could also be a variable, a function, and more. Here is an example of each:

//Global scope
var x = 12
{
    //local scope
    var y = 12;
}

So, notice that the scope of y is within the curly braces. Curly braces are one of the defining factors in the various levels of scope. Curly braces define a code block.

Practice

Let's look ahead at how let deals with scope:

  1. In 03-scope.js, declare a variable named "outside" using the var keyword; initialize it with the string: "outside".

  2. Add a code block beneath your first variable

  3. Inside the block declare a variable named "inside" using the let keyword; initialize it with the string: "inside". Inside the code block print the "inside" variable to the console.

  4. Outside of the block of code, print both variables to the console. What happens? The reasons for this will be covered more in-depth in the next module.

PreviousDeclarationsNextHoisting

Last updated 7 years ago