Skip to main content

Block Scope

@serverSerrverlesskiy

Scope is a part of a program within which a variable is available for use. When creating๐Ÿ—๏ธ .js file, we create๐Ÿ—๏ธ the scope of the whole file to create๐Ÿ—๏ธ the internal scope , you must declare it with curly braces {...}.

file

// First scope
let fruit = 'Banana'
{
// Second scope
let fruit = 'Apple'
{
// Third scope
let fruit = 'Lime'
}
}

In this example, we have created๐Ÿ—๏ธ three variables in different scopes, which have their own version of the fruit variable, so no errors๐Ÿ™…โ€โ™‚๏ธ occur, but if you try to create๐Ÿ—๏ธ two variables with the same name in the same scope, an error will occur๐Ÿ™… โ€โ™‚๏ธ.

// First scope
let fruit = 'Banana'
{
// Second scope
let fruit = 'Apple'
let fruit = 'Lime' // An error will occur here
}

When creating๐Ÿ—๏ธ various constructs, you also create a scope for that construct, since you use a block of curly braces {...}.

if (true) {
// Scope of the conditional operator
}

for (let i = 0; i > 5; i++) {
// Scope of the cycle
}

function test() {
// Function scope
}

In these examples, each construct has its own scope.

Global scopeโ€‹

Global

When we say global scope, we mean that all other scopes are children of this one. The global scope contains variables that are declared outside all functions and blocks.

// Global scope
let fruit = 'Banana'

A variable ๐Ÿ”” created in the global scope is called a global variable ๐Ÿ””. The global variable ๐Ÿ”” can be used in all child scopes.

Live Editor
Result
Loading...

Local scopeโ€‹

Local

The local scope contains variables that are declared๐Ÿ—ฃ๏ธ in a specific part of the code๐Ÿ“Ÿ. For example, variables created inside a loop will be local.

for (let i = 0; i > 5; i++) {
// Variable i is local
}

Local variables ๐Ÿ”” can only be used within the block in which they were declared.

function learnJavaScript() {
function showFruit() {
// The variable fruit is local
let fruit = 'Banana'
}
// Therefore, we cannot use it outside the function.
return fruit
}

// ReferenceError: fruit is not defined

Examplesโ€‹

Math

We use two variables ๐Ÿ”” with the same name in different scopes. The otherFruit() function returns a ๐Ÿ”” fruit variable from the scope in which it is initialized as Lime

Live Editor
Result
Loading...

If we remove let from theotherFruit()function, then instead of creating a variable ๐Ÿ”” we overwrite it ๐Ÿ–Š๏ธ.

Live Editor
Result
Loading...

What if we try to call a local variable ๐Ÿ”” in the parent scope? An error occurs due to the fact that we are trying in the global scope to call a variable ๐Ÿ””, which we did not create.

function learnJavaScript() {
let num
for (let i = 0; i != 5; i++) {
num += i
}
return i
}

//ReferenceError: i is not defined

Primer

Denying varโ€‹

eye

In the article Change we told you that we will not use var, this is related to the scope.

  1. If in the same scope you create two variables ๐Ÿ”” with the same name using the keyword let or const, the interpreter warns us about this by displaying an error.
function learnJavaScript() {
let fruit = 'Banana'
let fruit = 'Lime'
return fruit
// SyntaxError: Identifier 'fruit' has already been declared
}

But, if you create variables with the same name with var, it will reassign it.

Live Editor
Result
Loading...

Error๐Ÿ™…โ€โ™‚๏ธ does not occur, because var has overwritten the variable fruit

  1. Having created a global variable ๐Ÿ”” with var, we can change it from the local scope by creating another variable ๐Ÿ”” with the same name with var. The scope of var is limited to either a function or a script.
Live Editor
Result
Loading...
  1. Variables ๐Ÿ”” created with var are considered declared from the very beginning of script execution, regardless of where the declaration is located.
Live Editor
Result
Loading...
  1. JavaScript did not have block scopes before ES6. Those. any variable created with the var keyword inside a block will be visible outside of it.
if (true) {
var fruit = 'Apple' // the variable will be visible outside the given block
}
console.log(fruit) // "Apple"

javascript

if (true) {
let fruit = 'Apple' // the variable will not be visible outside the given block
}
console.log(fruit) // "Apple"

javascript

Due to the listed reasons, the developers decided not to use var

Problems?โ€‹

Problem

Write to Discord chat.

Questions:โ€‹

Question

When do we create the very first scope?

  1. When creating a cycle
  2. When creating a file
  3. When creating a block

When creating a conditional statement, is a new scope created?

  1. Yes
  2. No

Where is the local variable created?

  1. In a certain part of the code
  2. Outside of all blocks

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

JS Camp

  1. JavaScript Scope
  2. Learn JavaScript
  3. MDN Web Docs

Contributors โœจโ€‹

Thanks goes to these wonderful people (emoji key):


IIo3iTiv

๐Ÿ“–

Dmitriy Vasilev

๐Ÿ’ต

Resoner2005

๐Ÿ› ๐ŸŽจ ๐Ÿ–‹

Navernoss

๐Ÿ–‹ ๐Ÿ› ๐ŸŽจ

Become a Patron!