Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views1 page

Scope Closure

The document explains scoping in programming, detailing three types: Global Scope, Local Scope, and Block Scope, with examples for each. It also introduces the concept of Closure (or Lexical Scope) in JavaScript, illustrating how inner functions can access variables from outer functions. Overall, it emphasizes the importance of variable accessibility within different scopes in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Scope Closure

The document explains scoping in programming, detailing three types: Global Scope, Local Scope, and Block Scope, with examples for each. It also introduces the concept of Closure (or Lexical Scope) in JavaScript, illustrating how inner functions can access variables from outer functions. Overall, it emphasizes the importance of variable accessibility within different scopes in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

I. Scoping in programming scoping refers to the accessibility of the variable.

There are mainly 3 types of scoping which are as follows:


1. Global Scope
2. Local Scope
3. Block Scope

1. Global Scope:
When a variable is declared outside of a function, it is in the global scope and
its value is accessible throughout the program. Example:

var myGlobalVar = "This is global!";

function printSomething() {
console.log(myGlobalVar);
}

printSomething(); // Output: This is global!

2. Local (or Function) Scope:


Variables declared within a function are in the local scope, and they cannot be
accessed outside of that function. Example:

function printSomething() {
var myLocalVar = "I'm local!";
console.log(myLocalVar);
}

printSomething(); // Output: I'm local!

// Not allowed
console.log(myLocalVar); // Output: Uncaught ReferenceError: myLocalVar is not
defined

3. Block Scope
Block level scoping, meaning the variable is only accessible within the block it’s
defined i.e in conditional statement block or within loop statement block Example:

if (true) {
let blockScopedVar = "I'm block scoped!";
console.log(blockScopedVar); // Output: I'm block scoped!
}

console.log(blockScopedVar); // Uncaught ReferenceError: blockScopedVar is not


defined

II. Closure(or Lexical Scope) in javascript refers to the variable, which is


defined in the outer function, is accessible to the inner function which is also
defined within the outer function. Example:

function bankRobbery() {
const heroes = ["Spiderman", "Wolverine", "Black Panther", "Batwoman"];
function cryForHelp() {
for (let hero of heroes) {
console.log(`PLEASE HELP US, ${hero.toUpperCase()}`);
}
}
cryForHelp();
}
bankRobbery();

You might also like