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.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


IIo3iTiv

📖

Dmitriy Vasilev

💵

Resoner2005

🐛 🎨 🖋

Navernoss

🖋 🐛 🎨

Become a Patron!