Any argument will do?

Tell us what’s happening:

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

Link to the challenge:

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.

1 Like

smh… i was caught up on the instructions from the last challenge. thanks!

Hey @levithehandyman,

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.

1 Like

@catalactics – super helpful. thank you for taking the time to explain it clearly

Just keep in mind that if what you are getting passed as an argument is of type object you can mutate its values through the parameter.

const numbers = [1, 2, 3, 4];

function changeAtIndex(arr, idx) {
  arr[idx] = 100;
}

changeAtIndex(numbers, 0);
console.log(numbers); // [ 100, 2, 3, 4 ]
const user = {
  name: 'John'
}

function changeProperty(obj, value) {
  obj.name = value
}

changeProperty(user, 'Jack')
console.log(user); // { name: 'Jack' }