JavaScript Coding Day 2 | Global Scope Variables & Local Scope Variables

Global Scope Variables &  Local Scope Variables

Watch the lesson tutorial  🔻

🌎 Global Scope Variables

A global scope variable is declared outside of any function. It is accessible and can be modified from anywhere in your code, including inside functions.

Example 1: Global Variable Access

The variable a is declared outside of any function, making it a global variable. It is then accessible and logged to the console by the script.

CodeOutputReasoning
var a = 10; console.log(a);10The variable a is declared in the global scope. The console.log(a) statement is also executed in the global scope and successfully retrieves the value 10.
HTML
<script>
var a = 10;
console.log(a); // Output: 10
</script>

🏡 Local Scope Variables

A local scope variable is typically declared inside a function. It is only accessible within that function (and any nested functions). Outside the function, the variable does not exist.

Example 2: Accessing Global Variable from Inside a Function

The variable a is declared in the global scope. Even though console.log(a) is inside the function f1, the function can look up to the global scope and access the variable.

CodeOutputReasoning
var a = 10; function f1() { console.log(a); } f1();10The variable a is a global variable. JavaScript follows the scope chain: when f1 runs, it first looks for a in its local scope. Since it doesn't find it, it moves up to the global scope, finds a = 10, and successfully logs it. The function call f1() is required to execute the code inside the function.
HTML
<script>
var a = 10; // Global Variable

function f1() {
    console.log(a); 
}

f1(); // Calling the function is necessary to see the output
// Output: 10
</script>

Example 3: Variable Defined and Used Only Inside a Function

The variable a is declared inside the function f1, making it a local variable. Attempting to access it outside the function results in an error because it is confined to the function's scope.

CodeOutputReasoning
function f1() { var a = 10; } f1(); console.log(a);ReferenceError: a is not definedThe variable a is declared with var inside the function f1(), giving it local function scope. Once the function finishes executing, a ceases to exist. The final console.log(a) is executed in the global scope and cannot find a, leading to a ReferenceError. The function call f1() is necessary but doesn't make a global.
HTML
<script>
function f1() {
    var a = 10; // Local Variable
}

f1(); // Function executes, a is created and destroyed
console.log(a); 
// Output: ReferenceError: a is not defined
</script>

🔑 Key Takeaways

  • Global Variables are accessible everywhere. Use them sparingly to avoid naming conflicts.

  • Local Variables (declared inside a function using var, let, or const) are protected and only exist within the function. This is preferred for writing safe and modular code.

  • When a variable is accessed, JavaScript first looks in the current (local) scope and then moves up the scope chain to find it (like in Example 2). If it reaches the global scope and still can't find the variable, it throws a ReferenceError (like in Example 3).


- by Chirana Nimnaka

Comments

Popular posts from this blog

Python Coding Day 1 | The print() Function and Comments

What is Python?

Set Up an Environment to Code in Python