Search⌘ K
AI Features

Scope in JavaScript

Explore how scopes in JavaScript limit variable access and control their lifecycle. Understand global and local scopes, including function and block scope, to write secure and maintainable code. This lesson clarifies how different declarations affect scope and variable accessibility within a program.

Scopes restrict access and visibility of variables across a program.

The illustration gives an overview of how the scope can look like in a program. Now, let’s discuss properties.

Scope features

A scope has the following properties:

  • It limits the visibility and accessibility of a variable.
  • It controls the life of the variable and the assigned resources.
  • It helps secure resources and makes debugging easier.

With these features in mind, let’s explore scopes JavaScript.

Scopes in JavaScript

JavaScript has two scopes:

  1. Global scope
  2. Local scope

We will look at them individually.

Global scope

Global scope is the outermost scope in the JavaScript program. In this scope, all variables are accessible by any ...