Learn JavaScript: Scope

Learn about global and block level scope in JavaScript.

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

JavaScript block scoped variables

const isLoggedIn = true; if (isLoggedIn == true) { const statusMessage = 'User is logged in.'; } console.log(statusMessage);

const and let are block scoped variables, meaning they are only accessible in their block or nested blocks. In the given code block, trying to print the statusMessage using the console.log() method will result in a ReferenceError. It is accessible only inside that if block.

Global variables in JavaScript

const isLoggedIn = true; if (isLoggedIn == true) { const statusMessage = 'User is logged in.'; } console.log(statusMessage);

JavaScript variables which are declared outside of blocks or functions can exist in the global scope, which means they are accessible throughout a program. Variables declared outside of smaller block or function scopes are accessible inside those smaller scopes.

It is best practice to keep global variables to a minimum, unless they must be shared across multiple blocks or functions.

Scope
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 [...] statements. A block is the code found inside a set of curly braces [...

  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 b...

  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 is the idea in programming that some variables a...

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

Learn JavaScript: Scope

Start[missing "en.views.course_landing_page.introduction-to-javascript.course_illustration" translation]