Variable Scopes in JavaScript
JavaScript·2 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();Unlock the program 🚀
Pay once, own it forever.
€79
30-day money-back guarantee
- 13 modules
- 113 lessons with full-code examples
- 29 projects with commented solutions
- All future lesson and project updates
- Lifetime access
By submitting this form, you agree to the Terms & Conditions and Privacy Policy.