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 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”
Changing variables from outside
  • 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 directly access local variables from outside of it. However, a function can access variables declared in an enclosing scope.

If we access 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 variable names are looked up in the current scope and then in enclosing scopes.

var food = 'salad';

var eatDinner = function () {
  // This function does not declare any local variables.
  console.log('I ate ' + food);
};

eatDinner();
// The function reads 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 functions can access it unless a closer scope declares the same name. In other words, it can be used inside a function with no closer variable of the same name. 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, unless a closer scope declares a variable with the same name.

If a variable is accessed from inside a function, but this variable has not been declared in any accessible 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. In the console output inside the function body, replace the variable name food with soup.

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.