2.4 JavaScript Scope¶
Global Scope¶
Task 1: Declare a new variable called myVar
, and assign 100 to it. Display myVar
value in the console.
var myVar = 100;
console.log(myVar);
The result is: 100
Task 2: Create a new function that displays the value of myVar
. Do not forget to call this function.
var myVar = 100;
function displayVar(){
console.log(myVar);
}
displayVar();
The result is: 100
How does the code above work?
- The JavaScript engine reads the code from top to bottom.
- It declares and initializes the function
displayVar
. - It sets
myVar
toundefined
. - Now, the JavaScript engine executes the code line by line.
- It assigns 100 to
myVar
. - It runs the code inside the function, and display 100 in the console.
As you can see, myVar
is declared in the main area of our code. We could access the value of myVar
from the code inside displayVar
function.
Scope
The scope is the place in the code that you can access a variable from.
Global Scope
It is the area of the code that can be accessible from anywhere in your code.
Task 3: Check the following code, and try to understand the result.
displayVar();
function displayVar(){
console.log(myVar);
}
var myVar = 100;
The result is: undefined
How does the code above work?
- The JavaScript engine reads the code from top to bottom.
- It declares and initializes the function
displayVar
. - It sets
myVar
toundefined
. - Now, the JavaScript engine executes the code line by line.
- It calls the function
displayVar
, and it displaysundefined
in the console - It assigns 100 to
myVar
.
Task 4: Refer to task 3; is myVar
declared in the global scope?
Yes, myVar
is declared in the global scope, and it can be accessible from anywhere in our code.
Task 5: Declare a variable called username
in the global scope. Access and display this variable in a function.
var username = "Nawras";
function displayUsername(){
console.log(username);
}
displayUsername();
The result will be: Nawras
Note
You can access global scope variables inside the functions.
Task 6: Check the following code, and understand the result.
function displayVar(){
console.log(myVar);
}
var myVar = 100;
displayVar();
The result will be: 100
How does the code above work?
- The JavaScript engine reads the code from top to bottom.
- It declares and initializes the function
displayVar
. - It sets
myVar
toundefined
. - Now, the JavaScript engine executes the code line by line
- It assigns 100 to
myVar
. - It calls the function
displayVar
. It displays 100 in the console
Task 7: Refer to task 6; is myVar
declared in the global scope?
Yes, myVar
is declared in the global scope, and it can be accessible from anywhere in our code.
Local Scope¶
Task 8: Check the following code, and understand the result.
function setUsername(){
var username = "Ali";
}
setUsername();
console.log(username);
The result is: Uncaught ReferenceError: username is not defined
How does the code above work?
- The JavaScript engine reads the code from top to bottom.
- It declares and initializes the function
setUsername
. - Now, the JavaScript engine executes the code line by line
- It calls the function
setUsername
. - It assigns "Ali" to
username
. The variableusername
is only accessible inside the functionsetUsername
. - It tries to access
username
outside the function, but it fails, why?!
Local Scope Variables
Local scope variables are declared within the function, and they are only accessible within the function.
Note
You can NOT access local scope variables outside their function.
Task 9: Check the following code, and understand the result.
displayHello("Nawras");
console.log(greet);
function displayHello(name){
var greet = "Hello " + name;
}
The result is: Uncaught ReferenceError: greet is not defined
How does the code above work?
- The JavaScript engine reads the code from top to bottom.
- It declares and initializes the function
displayHello
. - Now, the JavaScript engine executes the code line by line
- It calls the function
displayHello()
with the value "Nawras". - It assigns "Hello Nawras" to
greet
. The variablegreet
is only accessible inside the functiondisplayHello()
. - It tries to access
greet
outside the function, but it fails because the variablegreet
is local to the functiondisplayHello
Note
The scope of the variable greet
is also called the function scope because it is only accessible within the function.
Task 10: Check the following code, and understand the result.
function setUsername(){
var username = "Ali";
console.log(username);
}
var username = "Nawras";
console.log(username);
setUsername();
console.log(username);
The result is as follows:
Nawras
Ali
Nawras
How does the code above work?
- The JavaScript engine reads the code from top to bottom
- It declares and initializes the function
setUsername
. - It assigns
undefined
to the variableusername
. - Now, the JavaScript engine executes the code line by line.
- It assigns "Nawras" to
username
. - It searches for a variable called
username
in the global scope. - It displays the value of the global
username
, which is "Nawras". - It calls the function
setUsername()
. - It assigns "Ali" to the local
username
. console.log(username)
: it searches forusername
in the function scope, it findsusername = "Ali"
, so "Ali" is displayed.- It searches for a variable called
username
in the global scope. - It displays the value of the global
username
, which is "Nawras".
Scope Chain
When a variable is used, the JavaScript engine tries to find this variable in the nearest scope.
If this variable is used within a function, the JavaScript tries to find this variable in the local function scope. If it does not exist in the local scope, it searches for this variable in the global scope. If it does not exist in the global scope, it raises a ReferenceError.
This process is called Scope Chain.
Task 11: What is the result of the following code?
function multiplyByTwo(num){
var result = num * 2;
console.log(result);
}
var result = 5;
console.log(result);
multiplyByTwo(6);
The result is as follows:
5
12
Task 12: Refer to task 11; why is 5 displayed first in the result?
The JavaScript searches for the variable result
in the nearest scope, which is the global scope. In the global scope, result
value is equal to 5.
Task 13: Refer to task 11; why is 12 displayed secondly in the result?
The JavaScript searches for the variable result
in the nearest scope, which is the function scope. In the function scope, result
value is equal to 12.
Task 14: What is the result of the following code?
function multiplyByTwo(num){
console.log(result);
}
var result = 5;
console.log(result);
multiplyByTwo(6);
The result is as follows:
5
5
Task 15: Refer to task 14; why is 5 displayed first in the result?
The JavaScript searches for the variable result
in the nearest scope, which is the global scope. In the global scope, result
value is equal to 5.
Task 16: Refer to task 14; why is 5 displayed secondly in the result?
The JavaScript searches for the variable result
in the nearest scope, which is the function scope. In the function scope, result
is not defined. Then, it searches for result
in the global scope. In the global scope, result
value is equal to 5.
We will learn about the block and lexical scopes later in this course.