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 JavaScript Variables
  • Local Scope
  • More on scope
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 4-Functions

Scope

Scope refers to the variables you have access to at various levels in your application. The {} are there to help you define scope in your files. Let's look at two important types of scope: local and global.

File Location

We will be working in the following file:

    javascript-library
        └── 1-Fundamentals
            └── 4-Functions
                04-scope.js <----You will be working in this file.

Global JavaScript Variables

A variable declared outside a function, becomes GLOBAL. All scripts and functions on a web page can access it.

var carName = " Volvo";

// code here can use carName

function myFunction() {
    // code inside this function can use the variable  
    carName = "Lamborghini";
}

Local Scope

Variables defined inside a function cannot be accessed from anywhere outside the function. Take the following example:

function myFunction() {
    var carName = "Volvo";
    // code here can use carName
}
console.log(carName)//Throws an error because the carName variable is not accessible here

Again, the carName variable is defined and accessible only in the scope of the function.

More on scope

There is a lot more to know on scope, and we will discuss it as we move forward.

PreviousCalling FunctionsNextParameters

Last updated 7 years ago