HTML Academy
Global scope
Getting to know events18/25
Back to the list of tasks
  • 1. Introduction to events
  • 2. How to add a handler
  • 3. How event handlers work
  • 4. Default actions
  • 5. Please pass the function
  • 6. Hiding the popup
  • 7. Pressing a key
  • 8. Choosing a key
  • 9. With one key press
  • 10. Summary of “Events in JavaScript”, part 1
  • 11. First program: “Don’t be shy”
  • 12. Welcome to our photo gallery
  • 13. Click ’em all!
  • 14. Adding an image
  • 15. A bug has crept into the system
  • 16. Scope
  • 17. Global scope
  • 18. Changing variables from outside
  • 19. Becoming Independent
  • 20. Closures
  • 21. Let’s prepare for school
  • 22. Fixing the gallery
  • 23. Getting to the heart of the matter
  • 24. Summary of “Events in JavaScript”, part 2
  • 25. The Second Program: “Señor Tomato”
Becoming Independent
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Changing variables from outside

Due to the fact that variables are searched for from the inside out, variables declared in an enclosing scope and used inside the function can be reassigned from outside the function.

Imagine that you are a school-age student, and the lunch bell has finally rung. You head for the cafeteria. Today they are serving Salisbury steak for lunch. And instead of pasta, the side is mashed potatoes! You have lunch and go back to class. After many more class periods and after-school activities, it’s now time for dinner. You head for the dining room, but instead of Salisbury steak, this time you are served only celery for dinner. And so you have the celery, since there is nowhere else you can go to eat. Let’s put this example in code form.

var food = 'Salisbury steak with mashed potatoes';

var eatDinner = function () {
  console.log('I ate ' + food);
};

eatDinner();
// Outputs: I ate Salisbury steak with mashed potatoes.

// Assign a new value to the food variable.
food = 'celery';

eatDinner();
// Outputs: I ate celery.

At first the value of the global variable food was 'Salisbury steak with mashed potatoes'. We called the function, and it used this value because it was the current one at that time. Then the value of food was changed to 'celery'. From this point in the code, the variable no longer has the old value. Therefore, when we call the function after the variable is reassigned, the function will use the new value.

Comments

  • script.js
JavaScript
var food = 'pies'; var eatDinner = function () { console.log('I ate ' + food); };

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. After the eatDinner function is defined, call this function.
  2. Change the value of the variable food to 'vinaigrette salad' later in the code after the function call.
  3. After that once again call the eatDinner function.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.