Key Concepts

Review core concepts you need to learn to master this subject


function myFunction() { var pizzaName = "Volvo"; // Code here can use pizzaName } // Code here can't use pizzaName

Scope is a concept that refers to where values and functions can be accessed.

Various scopes include:

  • Global scope (a value/function in the global scope can be used anywhere in the entire program)
  • File or module scope (the value/function can only be accessed from within the file)
  • Function scope (only visible within the function),
  • Code block scope (only visible within a { ... } codeblock)
Lesson 1 of 1
  1. 1
    An important idea in programming is scope. Scope defines where variables can be accessed or referenced. While some variables can be accessed from anywhere within a program, other variables may on…
  2. 2
    Before we talk more about scope, we first need to talk about blocks. We’ve seen blocks used before in functions and if statements. A block is the code found inside a set of curly braces {}. Blo…
  3. 3
    Scope is the context in which our variables are declared. We think about scope in relation to blocks because variables can exist either outside of or within these blocks. In global scope, varia…
  4. 4
    The next context we’ll cover is block scope. When a variable is defined inside a block, it is only accessible to the code within the curly braces {}. We say that variable has block scope becaus…
  5. 5
    It may seem like a great idea to always make your variables accessible, but having too many global variables can cause problems in a program. When you declare global variables, they go to the _gl…
  6. 6
    Given the challenges with global variables and scope pollution, we should follow best practices for scoping our variables as tightly as possible using block scope. Tightly scoping your variables …
  7. 7
    In this lesson, you learned about scope and how it impacts the accessibility of different variables. Let’s review the following terms: - Scope refers to where variables can be accessed throug…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo