HTML Academy
Scope
Getting to know events17/25
Back to the list of tasks
  • 1. Introduction to events
  • 2. How to add a handler
  • 3. How events are arranged
  • 4. Default actions
  • 5. Please pass the function
  • 6. Hiding the popup
  • 7. Pressing a key
  • 8. Choosing a key
  • 9. With one click
  • 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. Inside out variables
  • 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”
Inside out variables
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Global scope

The local scope is limited by the function, so we cannot obtain function variables from outside of it. However, everything works differently from inside the function as opposed to outside of it.

If we call some non-local variable from inside the function, JavaScript will move one level higher and search outside the scope of the function to find the desired variable. It is a known principle that variables are read from inside the current scope to the outside.

var food = 'salad';

var eatDinner = function () {
  // The local variables are not declared inside the function.
  console.log('I ate ' + food);
};

eatDinner();
// The function calls the food variable,
// which is declared outside of eatDinner
// Outputs: I ate salad.

In our example, the food variable is not declared inside a particular function. It is declared at the level of the entire program, which means that it can be seen from everywhere. In other words, it can be used inside every function. These variables, which are declared at the very top level, outside of all particular functions, are called global. And the scope in which they can be found is called the global scope. We can tell from their name that the variables from this scope can be seen throughout the entire program code, and they are available in all blocks of code.

If a variable is called from inside a function, but this variable has not been declared in any scope, the console will output an error.

Comments

  • script.js
JavaScript
var food = 'borscht'; 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 declared, call this function.
  2. Change the food variable to soup in the function body inside the console string output.

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

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.