JavaScript Scope
One of the key concepts in JavaScript is scope - it refers to the area of a program where variables and functions are accessible. Understanding scope is crucial for writing efficient, maintainable error-free code.
Here are the different types of scope available and how they work:
Global Scope
Global scope refers to variables or functions that are accessible throughout the entire program. Variables and functions declared outside of any function have global scope.
var greeting = "Hello World!";
function sayHello() {
console.log(greeting);
}
sayHello(); // prints: "Hello World!"
In this example, greeting
is declared outside of any function, making it a global variable that can be accessed from anywhere in the code.
Local Scope
Local scope refers to the area of a program where variables and functions are available - they are only accessible within a specific function or block of code.
Function Local Scope
In the example below, the greeting
variable is defined inside the sayHello
function and only accessible within it. The attempt to access it outside the function results in a ReferenceError
:
function sayHello() {
var greeting = "Hello World";
console.log(greeting); // prints: "Hello World"
}
sayHello();
console.log(greeting);
// Uncaught ReferenceError: greeting is not defined
The same outcome will occur when using let
instead of var
.
Block Local Scope
As mentioned in JavaScript Variables and Constants, let
is block scoped - it will only be available inside the block of code it is defined in. Below is an example showcasing the difference between var
and let
scoping:
var greeting = "Hello World"
if(true) {
let greetingPaul = "Hello Paul";
console.log(greeting); // prints: "Hello World"
console.log(greetingPaul); // prints: "Hello Paul"
}
console.log(greeting); // prints: "Hello World"
console.log(greetingPaul);
// Uncaught ReferenceError: greetingPaul is not defined
Here is another, slightly more convoluted example:
function sayHello() {
let name = "John"
console.log(`Hello ${name}`)
if(true) {
let name = "Paul"
console.log(`Hi ${name}`)
}
console.log(`Hello again ${name}`)
}
sayHello()
We declare a variable name
inside the sayHello
function and assign it the value John
. Then, we declare another variable, with the same name, name
, inside the if
block and assign it the value Paul
. The output will be:
"Hello John"
"Hi Paul"
"Hello again John"
Notice that the 3rd message includes the value of the first name
variable.
Nested Local Scopes
JavaScript also allows nested local scopes, which means that a function can have its own local scope, and any nested function inside that function can also have its own local scope. The inner function can access the variables declared in the outer function, but the outer function cannot access the variables declared in the inner function.
function outerFunction() {
let outerVar = "I am in outer function";
function innerFunction() {
let innerVar = "I am in inner function";
console.log(outerVar); // Output: "I am in outer function"
}
innerFunction();
console.log(innerVar);
// Output: Uncaught ReferenceError: innerVar is not defined
}
outerFunction();
In the above example, the outerFunction()
has a local variable outerVar
, and it also has an inner function innerFunction()
, which has its own local variable innerVar
. The inner function can access the outerVar
variable, but the outer function cannot access the innerVar
variable.
Take Away
Local scope is essential in JavaScript because it helps prevent naming conflicts between variables and functions. By limiting the accessibility of variables and functions to specific parts of the code, it makes it easier to manage and debug code. It also helps keep the code organised and modular since each function can have its own local scope.