HTML Academy
A bug has crept into the system
Getting to know events16/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”
Global scope
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Scope

A bug was detected! The index of the last element in the photos array (4) and the value of i in our code (5) for any of the thumbnails we could click on. We do not have any element with this index in the photos array. Therefore, we receive undefined instead of the image address, and no image appears. But what is the real reason this is happening?

Let’s step back a bit from our assignment and explain one particularly important concept in JavaScript. This will help us solve the problem that we are having with our photo gallery.

Write the eatDinner function. It will output messages about our meal to the console. It has the drink parameter and the food variable.

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

eatDinner('fruit juice');
// Outputs: I ate pasta.
// Outputs: I drank fruit juice.

console.log('I ate ' + food);
// Outputs an error.

console.log('I drank ' + drink);
// Outputs an error.

If we try to access the food variable from outside the scope, we won’t succeed: the console will output an error. Because variables from the function body are only available inside this function. There is no way that you can access them from outside the scope.

The same thing will happen if we want to access the function parameter from outside the scope. The parameter, although it is set externally, behaves like a variable inside the function.

Why is that?

Because each function has a scope, which is defined as all of the values that are available to this function. The scope is limited to the function itself. Since the food variable has been declared inside the eatDinner function, it is accessible only inside the scope of this function, like the drink parameter. These variables are called local variables of the function. Their scope is limited by the function in which they are declared, and these variables cannot be obtained outside of this scope. Therefore, this scope is also called local.

Let’s prove this to ourselves. Call the function, and then try to access a parameter and variable from the body of the eatDinner function from outside of its scope.

Comments

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

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. Below that, after the eatDinner function is declared, call this function with the argument 'tea'.
  2. After this function is called, output the string 'I ate ' + food to the console.
  3. Replace the string with 'I drank ' + drinkin this console 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.