In this lab, you’ll practice working with variable scope. You'll learn the differences between global scope, function scope, and block scope, as well as the behavior of variables declared using var, let, and const.
In scope.js, complete each drill by writing the JavaScript code that satisfies the following requirements. Make sure to test your code by checking the console to understand how scope affects the variables.
-
Global Variable
Declare a variablexin the global scope, and print it to the console. Then, declare a variableyinside a function and try to print it outside the function. What happens? -
Function Scope with
var
Inside a function, declare a variable usingvarand print it both inside and outside the function. What happens when you try to access it outside the function? -
Function Scope with
let
Inside a function, declare a variable usingletand print it both inside and outside the function. What happens when you try to access it outside the function?
-
Block Scope with
letandconst
Inside aforloop, declare a variable usingletandconst, and print the value of the variable inside the loop. What happens if you try to access the variable outside the loop? -
Nested Block Scope
Declare a variable inside anifblock usingletand try to access it outside of the block. Do the same for avarvariable. How do their behaviors differ?
-
Scope Chain
Inside a function, declare a variablea. Then, inside an inner function, declare a variableb. Try to access both variables from the inner function. What happens if you try to accessafrom the outer function? -
Nested Function Scope
Declare a variable inside an outer function. Then, inside a nested inner function, declare a variable and try to access both variables from the inner function. What do you observe?
-
Function Parameter Shadowing
Create a function with a parameter that has the same name as a variable outside the function. Try to access the parameter and the variable inside the function. What happens to the variable inside the function? -
Global Variable Overwritten by Function
Declare a global variable, then create a function that reassigns it. Print the value of the global variable both before and after calling the function. What happens to the global variable after the function is executed?
These are advanced topics. You will need more experience to tackle these
-
Closure Example
Create a function that returns another function (a closure). The inner function should access a variable declared in the outer function. Try calling the inner function from outside and observe how the variable is accessible. -
Immediate Invoked Function Expression (IIFE)
Create an IIFE that declares a variable and prints it to the console. Try accessing the variable outside the IIFE. What happens?
- Complete all drills in the file scope.js.
- Test each drill by running your code and checking the output in the console.
- Push your completed work to your repository and submit the link on Google Classroom.
- Be prepared to explain how your code works during a follow-up discussion.
Good luck and happy coding! 🚀