0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>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?</p>
1
<p>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?</p>
2
<p>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.</p>
2
<p>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.</p>
3
<p>Write the eatDinner function. It will output messages about our meal to the console. It has the drink parameter and the food variable.</p>
3
<p>Write the eatDinner function. It will output messages about our meal to the console. It has the drink parameter and the food variable.</p>
4
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.<p>If we try to access the food variable from outside the scope, we won’t succeed: the console will output an error. Because<b>variables from the function body are only available inside this function</b>. There is no way that you can access them from outside the scope.</p>
4
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.<p>If we try to access the food variable from outside the scope, we won’t succeed: the console will output an error. Because<b>variables from the function body are only available inside this function</b>. There is no way that you can access them from outside the scope.</p>
5
<p>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.</p>
5
<p>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.</p>
6
<p>Why is that?</p>
6
<p>Why is that?</p>
7
<p>Because each function has a <b>scope</b>, 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<i>local variables</i>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<i>local</i>.</p>
7
<p>Because each function has a <b>scope</b>, 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<i>local variables</i>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<i>local</i>.</p>
8
<p>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.</p>
8
<p>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.</p>