what is it that connects the global variable with any old name passed in as an argument?
im confused as to how the function incrementer connects whatever is passed in as an argument to the global var fixedValue. is this just the magic of JavaScript?
Your code so far
// The global variable
var fixedValue = 4;
// Only change code below this line
function incrementer (howDoesThisWork) {
return howDoesThisWork + 1;
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0.
Challenge: Pass Arguments to Avoid External Dependence in a Function
It doesn’t. That’s the point of this exercise. The incrementer function only works with the value passed into it. It is not dependent upon the global variable fixedValue.
The incrementor function takes an argument, which is named howDoesThisWork. This means if it’s called with an argument, it will take the argument and just simply create a new variable that points to the argument given, that is ONLY available inside the function. Let me explain it with code:
// Global value variable, it can be accessed anywhere in the JS document
var globalValue = 5;
function addFive(argument) {
// the function creates a new variable in the background that
// points to the argument given.
// so if globalValue is passed, in the background
// it is essentially doing
// var argument = fixedValue;
// now you can use the argument variable
// In this case it just returns the result
// of the variable argument + 5
return argument + 5;
}
addFive(globalValue);
// We give the function the global value variable
// inside it's going to make a temporary local variable
// that points to the variable.
This is just called a function argument, and it’s the magic of EVERY programming language that has functions not just JavaScript. Hope this helps you understand it a bit more now.