In this article, we will be discussing the accessibility of a given code, based on its scope declaration.
Definition of Variable Scope: Scope of a variable is the part of a program in which a code is accessible.
Basically we have 2 types of Variable scopes; Global scope and The Local scope.
Further more, a code can only be accessed in a program if it hasn't used up its life span. That is to say, every code has a given life span in which when exhausted, the code will be erased from the memory of the computer. The computer will not be able to access that code or run it.
GLOBAL SCOPE: a global variable scope is accessible throughout the entire program. In other words they have a very long life span. You can access a global variable scope anywhere throughout the program. They are citizen of the entire program. In the program, a globally declared variable scope takes dominance, but within a local variable scope, the locally declared variables are dominant(gets executed over the global variable scope).
You can access the global variable scope even in local scope like within a function. A global variable scope claims citizenship throughout the entire program.
For Example:
let a = gold;
Console.log(a);
Function local(){
let b = silver;
Console.log(a)
}
local();
When we run this code we get the word gold printed twice, though it wasn't declared within the function, but for the fact it's a globally declared variable scope, it can be accessed throughout the program, and have a long life span.
LOCAL SCOPE: A local variable scope is a code that is only accessible within the code block it was declared, within a function. It takes citizenship of only its code block or within the function it was declared, outside which the code ceases to exist, or its life span is terminated. A locally declared variable scope can not be accessed outside its code block, once the code block ends, the life span of the local scope ends as well, and it gets erased from the memory of the computer. That is to say, you can not call a locally declared variable outside its function or code block, it will return the error not declared.
However, within the function or the code block of a local variable scope, the local scope takes dominance over the global scope.
For Example:
let a = gold;
Function local(){
let a = silver;
Console.log(a)
}
local();
When we call the function local, it returns silver. Because within the local scope, the locally declared variable takes dominance over the global scope.
But when we call a local scope outside its code block, it can not be accessed. It will return the error not declared. That is, once the code block ends, the life span of a locally declared variable ends as well.
If we declare a variable locally with the same variable name as a globally declared variable, both variables are entirely different and will return either the local scope or global scope based on where it is called.
Example;
let a = gold;
Console.log(a);
Function local(){
let a = silver;
Console.log(a)
}
local();
When we run the above code, it will return gold, while the function will return silver.
Based on its scope, codes with same variable name can return different result.
Top comments (0)