Scope of Variables and Functions

Understand the scope of variables and functions to write maintainable JavaScript code.

In JavaScript, the scope of variables and functions determines their accessibility, or visibility, in different parts of a program. The scope can be classified into global scope, function scope, block scope, and module scope. Let’s dive into these types:

Global scope

Variables or functions declared in the global context are accessible throughout the entire program.

Let’s understand how to declare them:

  • Global variables: Variables declared with var, let, or const outside of any function or block.

  • Global functions: Functions declared outside of any other function or block.

Get hands-on with 1400+ tech skills courses.