0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>We found out that when we redefine global variables, it affects the output of the functions that use these variables. However, as a general rule of thumb it is not worth doing this. It is not a best practice. Redefining variables that are used by a function can lead to unexpected consequences and bugs in the code. That is why we have parameters that allow us to reuse functions whereby we explicitly pass the required values.</p>
1
<p>We found out that when we redefine global variables, it affects the output of the functions that use these variables. However, as a general rule of thumb it is not worth doing this. It is not a best practice. Redefining variables that are used by a function can lead to unexpected consequences and bugs in the code. That is why we have parameters that allow us to reuse functions whereby we explicitly pass the required values.</p>
2
<p>We already have a clear understanding of how parameters work if we want to immediately call a function and obtain the output of its execution.</p>
2
<p>We already have a clear understanding of how parameters work if we want to immediately call a function and obtain the output of its execution.</p>
3
var eatDinner = function (food) { console.log('I ate ' + food); }; eatDinner('steak'); // Outputs: I ate steak.<p>What should we do if we want to record a certain value in the function but obtain the result later? We do the same thing with events: we create a function, and we use certain values inside it, but we do not obtain an immediate result. It only happens when some event occurs. Of course, everything doesn’t always go smoothly, like we can see in the case of our gallery. However, we are getting closer to solving our problem.</p>
3
var eatDinner = function (food) { console.log('I ate ' + food); }; eatDinner('steak'); // Outputs: I ate steak.<p>What should we do if we want to record a certain value in the function but obtain the result later? We do the same thing with events: we create a function, and we use certain values inside it, but we do not obtain an immediate result. It only happens when some event occurs. Of course, everything doesn’t always go smoothly, like we can see in the case of our gallery. However, we are getting closer to solving our problem.</p>
4
<p>Let’s return to the example of the child at school. What if our student doesn’t want to eat celery? And he doesn’t want to be limited to the food that is served to him in the cafeteria? In that case, he will need to bring his own food with him from home! In that case, he will always have a lunchbox in his backpack with what he would prefer to eat. He can get it out at any time and have a bite to eat. How can we implement this in the code?</p>
4
<p>Let’s return to the example of the child at school. What if our student doesn’t want to eat celery? And he doesn’t want to be limited to the food that is served to him in the cafeteria? In that case, he will need to bring his own food with him from home! In that case, he will always have a lunchbox in his backpack with what he would prefer to eat. He can get it out at any time and have a bite to eat. How can we implement this in the code?</p>
5
var collectContainer = function () { var food = 'pasta'; var eatDinner = function () { console.log('I ate ' + food); } return eatDinner; }; var schoolkid = collectContainer(); // The variable contains the eatDinner function, // which the collectContainer function returned. // We call the eatDinner function from the schoolkid variable. schoolkid(); // Outputs: I ate pasta.<p>Don’t worry if you’re confused. In the next assignment, we will provide a detailed analysis of how this code works and why. In the meantime, let’s just make sure that this code actually executes as shown in the example.</p>
5
var collectContainer = function () { var food = 'pasta'; var eatDinner = function () { console.log('I ate ' + food); } return eatDinner; }; var schoolkid = collectContainer(); // The variable contains the eatDinner function, // which the collectContainer function returned. // We call the eatDinner function from the schoolkid variable. schoolkid(); // Outputs: I ate pasta.<p>Don’t worry if you’re confused. In the next assignment, we will provide a detailed analysis of how this code works and why. In the meantime, let’s just make sure that this code actually executes as shown in the example.</p>