What is scope & why do we need it?

What is scope & why do we need it?

Scope simply allows us to know where we have access to our variables. It shows us the accessibility of variables, functions & objects in some particular part of the code.

Why would we want to limit the visibility of variables to local / block scope instead of keeping them in global scope?

  • Firstly, it provides us with some level of security o our code.
  • Secondly, it helps to improve efficiency, track bugs & reduce them. It also solves the problem of naming variables.

We have three types of scope:

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

Global Scope :

  • when you start writing in Javascript document, you're already in the Global Scope.
    const name = "Test";
    
  • Variables written inside the Global Scope can be accessed & modified in any other scope.
  • We can also say that, variables that are declared outside the functions are under Global Scope.
    const name = "Global";
    const getName = () => console.log({name})
    getName();
    // output : {name : "Global"}
    

    Advantages of using Global Variables :

  • We can access the global variable in all the functions.
  • A Global variable is useful when multiple functions are accessing the same data.

    Disadvantages of using Global Variables :

  • Too many variables declared as global, then they'll remain in the memory until the execution is completed. This can cause memory shortage.
  • Value of the global variable can be modified in any other scope. This may give unpredictable results.

Local Scope :

  • Variables declared inside a function are under local scope.
// global scope
const printName = () => {
// local sope-1

const name = "John";
console.log({name});
// output : {name :   "John"}

const nestedFunction = () => {
// local scope-2

console.log({name });
// output : {name   : "John"}

const name = "Peter";
console.log({name});
// output : {name :   "peter"}
}
nestedFunction();
}
printName();
  • Here, the name in the printName fn can be accessed inside of the nestedFunction fn. Because, nestedFunction fn scope is :
  • It's own variables
  • It's parent function (printName)
  • Global Variables etc...

Advantages of using Local Variables :

  • Variables present in the local scope can't be accessed by the Global / Outer scope.
  • It prevents Memory Leakage.
  • Local variables are deleted as soon as the particular fn is executed. Thus, there is no shortage of memory.

Disadvantages of using local variables :

  • They have very limited scope. This isn't a disadvantage, but if you need any local variable to be accessible at the global socpe, then move that variable to the global scope.

Block Scope :

  • Block statements like if or for or while loops, unlike functions don't create a new scope.
  • Variables declared with the const & let keywords have the block scope & can't be accessed outside the scope unlike the variables declared using var keyword.
if(true) {
var name = "John";
let age = 20;
const address = "Nearby";

console.log({name});
console.log({age});
console.log({address});
// output : {name : "John"})
// output : {age: 20})
// output : {address : "Nearby"})
}
console.log({name});
console.log({age});
console.log({address});
// output : {name : undefined / ""})
// output : reference error (age is not defined).
// output :  reference error (address is not defined).
  • Hence, we can't access the age & address as they are declared using let / const.

Conclusion :

  • The local & global variables are equally important while writing a program in any language. However, a large number of the global variable may occupy a huge memory. An undesirable change made to global variable is become tough to identify.
  • Always declare variables in the nearest scope that you want to use them in.
  • Try not to declare variables using var or let unless there is a need to update the them.
  • Instead of var you can use let, because it provides block scope, which helps in avoiding memory leakage.

--> If you like My content feel free to like, comment& share it to others.

--> If there is any thing to be corrected, plz mention in the comment section below, I would appreciate that.