Arguments are Killing Me!

Hi,

I’ve been doing quite well with JS and I’m really enjoying it. My progress has been good and because I’m really getting into it now, my pace has picked up as my interest has grown, however, I seem to have hit a brick wall as far as arguments are concerned and it’s gotten to the point where they’re holding me back, I’m really stuck.

I get the basic concepts of arguments/parameters and that they’re basically placeholders for code you wanna pass through functions:

function add (a, b){ return a + b; }

You can then call add() and pass in whatever values you like and it will run the function on them add(5, 5) will return 10.

It’s when code becomes a little populated and figuring out what arguments go where and when they are used or needed is when my head starts to explode.

Has anybody else had a similar issue and how did you manage to resolve it?

Im pretty new too but will help if I can!. Can you post a snippet of the type of argument you describe that causes brain explosion? lol That will give me something concrete to help explain whats going on with the parts giving you trouble

And I feel you…sometimes Im feel like so much of my brain has leaked out of my ears, Im worried Ill slip on it and hurt myself :laughing:

Within the function, they’re essentially just variables. Keep track of them the same way you do any variables. Part of this is always giving them meaningful names.

I wouldn’t consider arguments code, arguments are essentially values that you want to pass to the function for further action. The arguments go into the () of the function. Inside the function you will write out the code to manipulate the values and change it into the output that you’ll return back to the executing code. This article (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) may be benefical for you to read as well.

A quick note: you can also pass a lot of arguments to a function for more complex tasks, you aren’t limited to just two arguments.
For example:

function mySuperCoolFunction(a, b, c, d, e, f, g, h) { return a + b - c * d + e - f / g + h; }

When looking into this question for you it that was suggested to make that if you are using a function with a lot of arguments an array of the arguments may be helpful to you. Some further reading on functions with a lot of arguments: https://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept

Hi.

I’m not actually stuck on a problem at the moment but the last task I did on here caused me some headaches:

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  if(myObj.hasOwnProperty(checkProp)){
    return myObj[checkProp];
  } else{
  
  return "Not Found";
  }
}

// Test your code by modifying these values
checkObj("gift");

That’s the solved version and it looks so simple afterwards, only checkProp needed to be passed through but before solving it, I actually thought I needed a for loop to iterate through myObj like so: for(var i = 0; i < myObj.length; i++, so I was trying to pass i as an argument but that obviously didn’t work and I tried to pass myObj and so on - for some reason it’s just not clear to me.

Obviously, I solved it in the end but my frustrations are mostly because of the arguments are not sinking in, I’m doing quite well with the rest but all these parentheses and brackets are distracting. Maybe we should just remove them from Javascript altogether - I think that would be best :grin:

Not a bad idea…if you start a petition to ECMA to have that removed, let me know…Ill sign it :laughing:

Yeah, I see what you mean though, I too have made that mistake…a few times even. More than anything its just a matter of using the right tool for the job,and indeed…a loop is not the right tool for this job. But, thats the cool thing about mistakes, next time you see this you’ll know better…or at the least even if you forget for a moment, instead of forever struggling why a loop isnt working, you’ll say oooh dang of course…gotta do checkProp with a string literal! Crisis avoided with brain intact :smiley:

Hiya!

As other people have replied, parameters to a function are just values bound to names. It’s kind of like the code inside the curly brackets {} was being run outside of a function, but the values of the arguments had been assigned to global variables of the same name.

IE
function add ( a, b ) { return a + b; } var sum = add ( 2, 4 );
is just like
var a = 2; var b = 4; var sum = a + b;

Functions are kinda just shorthand for longer pieces of code (if you ignore scoping rules).

That being said, I understand the frustration you’re feeling. In my experience, it gets a lot easier with practice, but I still sometimes find that I have to go back and look at a function to see what the order and meaning of each argument to a function. One thing that helps a lot with this is to put the function signature in a comment above where you’re about to call it, so you can see the names of the parameters you’re passing.

for example:

// setProperty ( target_object, property_name, value ) setProperty ( myPerson, "name", "jeremy" );

Here, it’s clear exactly what each argument means, because i can see by their names what was intended.

1 Like