In our theoretical apartment building, you have your own apartment. It has stuff in it that is yours. Other people in the building can’t access it. This is like functional scope. You have access to your stuff inside your apartment, and in the building – but not anyone else’s apartment.
When we write variables inside a function, only that function has access to its own variables. Therefore, they are in the functional scope.
In addition to a function having access to its own variables, it also has access to variables in the global scope.
In the last exercise we created both variables in the global scope. That is,
mailRoom are accessible from anywhere in our program.
Now, let’s make two variables within a functional scope.
Write a function named
myApartment before the
console.logs from the last exercise.
Inside of the function, write a variable named
mailBoxNumber and set it equal to
Also, you’re lucky enough to have in-unit laundry, so let’s re-assign
laundryRoom inside our function to:
laundryRoom = 'In-unit'.
Inside the function, use
console.log to print out both variables, like this:
console.log('Mail box: ' + mailBoxNumber + ', Laundry:' + laundryRoom);
Now, let’s try to see the
mailBoxNumber outside the function. On the last line of the program, write:
To see the hidden variables inside the function, delete the
mailBoxNumber in the global scope, and call the
myApartment function instead.