Summary: in this tutorial, you will learn about the JavaScript variable scope that determines the visibility and accessibility of variables.
What is variable scope
Scope determines the visibility and accessibility of a variable. JavaScript has three scopes:
- The global scope
- Local scope
- Block scope (started from ES6)
The global scope
When the JavaScript engine executes a script, it creates a global execution context.
Also, it also assigns variables that you declare outside of functions to the global execution context. These variables are in the global scope. They are also known as global variables.
See the following example:
var message = 'Hi';Code language: JavaScript (javascript)The variable message is global-scoped. It can be accessible everywhere in the script.