Variable Scopes in JavaScript
9 min read·Jan 1, 2026
In programming, the scope of a variable refers to the area of the code where a variable can be accessed and used.
In JavaScript, there are three main types of variable scope: global, local, and block.
Global variables
Global variables are variables that are accessible and modifiable from anywhere in the script, including within control statements and functions.
They are usually declared and initialized at the top of the script, and are used to store values that need to be shared across functions or for data that needs to maintain its state (or value) throughout the entire script execution, referred to as constants.
Example
Let's consider this script, that implements two functions named incrementCounter and resetCounter used to change the value of the global counter variable:
let counter = 0;
function incrementCounter() {
counter = counter + 1;
}
function resetCounter() {
counter = 0;
}
incrementCounter();
console.log(counter);
resetCounter();
console.log(counter);
When executed, it will:
- Declare a global variable named
counterand initialize it with0. - Define a function named
incrementCounterthat increases the value of the globalcountervariable by1. - Define a function named
resetCounterthat assigns0to the globalcountervariable. - Execute the
incrementCounterfunction and output the value of thecountervariable. - Execute the
resetCounterfunction and output the value of thecountervariable.
Which will produce this output:
$ node increment_counter.js
1
0
Local variables
Local variables are variables that are only accessible and modifiable from within the body of the function or loop they are defined in.
They are used to store temporary values that should be isolated from the rest of the script, and only exist for the duration of the function or loop execution.
Example
📚 Definition: In mathematics, the factorial of a non-negative integer
n(writtenn!) is the product of all positive integers less than or equal ton.n! = 1 x 2 x ... x nFor example: 3! = 1 x 2 x 3 = 6
Let's consider this script, that implements a function named factorial that calculates the factorial of a number:
function factorial(number) {
let result = 1;
for (let i = 1 ; i <= number ; i++) {
result = result * i;
}
console.log(result);
}
factorial(5);
console.log(result);
When executed, it will:
-
Define a function named
factorialthat takes as parameter a number.- Declare a variable named
resultand initialize it with1. - Start a
forloop going from1to the value of thenumberparameter. - Multiply the value of the
resultvariable by the current value of theicounter variable. - Output the value of the
resultvariable within the function's scope it is defined in.
- Declare a variable named
-
Execute the
factorialfunction with the integer number5. -
Attempt to output the value of the
resultvariable outside of the function's scope.
Which will produce this output:
$ node factorial.js
120
/Users/razvan/scripts/nodejs/factorial.js:12
console.log(result);
^
ReferenceError: result is not defined
As you can see here, the script will throw a ReferenceError error caused by an invalid access to the result variable outside of the factorial function's scope it was defined in.
Block variables
Block variables are variables that are only accessible from within the statement they are defined in, like an if statement or a for loop.
They are used to store temporary values, like counters, that should be isolated from the rest of the script.
Example
Let's consider this script, that implements a function named printMultiplicationTable that outputs the multiplication table of a number:
function printMultiplicationTable(number) {
for (let i = 1 ; i < 10 ; i++) {
console.log(number, 'x', i, '=', number * i);
}
console.log(i);
}
printMultiplicationTable(3);
When executed, it will:
-
Declare a new function named
printMultiplicationTablethat takes as parameter a number.- Start a
forloop going from1to10. - Output the value of the
numberparameter, the current value of theicounter variable, and the product of these variables. - Attempt to output the value of the
ivariable outside of theforloop scope.
- Start a
-
Execute the
printMultiplicationTablefunction with the integer number3.
Which will produce this output:
$ node print_multiplication_table.js
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
/Users/razvan/scripts/nodejs/print_multiplication_table.js:7
console.log(i);
^
ReferenceError: i is not defined
As you can see here, the script will throw a ReferenceError error caused by an invalid access to the i variable outside of the for loop's scope it was defined in.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery