0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>Due to the fact that variables are searched for from the inside out, the variables that are used inside the function can be redefined from the outside.</p>
1
<p>Due to the fact that variables are searched for from the inside out, the variables that are used inside the function can be redefined from the outside.</p>
2
<p>Imagine that you are a school-age student, and the lunch bell has finally rung. You head for the cafeteria. Today they are serving Salisbury steak for lunch. And instead of pasta, the side is mashed potatoes! You have lunch and go back to class. After many more class periods and after-school activities, it’s now time for dinner. You head for the dining room, but instead of Salisbury steak, this time you are served only celery for dinner. And so you have the celery, since there is nowhere else you can go to eat. Let’s put this example in code form.</p>
2
<p>Imagine that you are a school-age student, and the lunch bell has finally rung. You head for the cafeteria. Today they are serving Salisbury steak for lunch. And instead of pasta, the side is mashed potatoes! You have lunch and go back to class. After many more class periods and after-school activities, it’s now time for dinner. You head for the dining room, but instead of Salisbury steak, this time you are served only celery for dinner. And so you have the celery, since there is nowhere else you can go to eat. Let’s put this example in code form.</p>
3
var food = 'Salisbury steak with mashed potatoes'; var eatDinner = function () { console.log('I ate ' + food); }; eatDinner(); // Outputs: I ate Salisbury steak with mashed potatoes. // Let's redefine the food variable food = 'celery'; eatDinner(); // Outputs: I ate celery.<p>At first the value of the global variable food was 'Salisbury steak with mashed potatoes'. We called the function, and it used this value because it was the current one at that time. Then the value of food was changed to 'celery'. Henceforth from this place in the code, the variable no longer has the old value. Therefore, when we call the function<b>after</b>the variable is redefined, the function will use the new value.</p>
3
var food = 'Salisbury steak with mashed potatoes'; var eatDinner = function () { console.log('I ate ' + food); }; eatDinner(); // Outputs: I ate Salisbury steak with mashed potatoes. // Let's redefine the food variable food = 'celery'; eatDinner(); // Outputs: I ate celery.<p>At first the value of the global variable food was 'Salisbury steak with mashed potatoes'. We called the function, and it used this value because it was the current one at that time. Then the value of food was changed to 'celery'. Henceforth from this place in the code, the variable no longer has the old value. Therefore, when we call the function<b>after</b>the variable is redefined, the function will use the new value.</p>