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 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”
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) differs from 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);
// Throws an error. Test the next example separately.

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. This is because variables declared inside the function body are only accessible directly inside this function.

The same thing will happen if we want to access the function parameter from outside the scope. Although its value is passed as an argument from outside, the parameter is local to the function.

Why is that?

Each function has a local scope, which determines where its parameters and local variables can be accessed by name. 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 accessed directly from outside 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 expression 'I ate ' + food to the console.
  3. Replace the expression with 'I drank ' + drinkin this console output.

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.