Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Passing Arguments" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

Bianca demonstrates how functions passed as arguments can be invoked using their parameter name. These function can also have their own set of arguments passed to them when they are invoked.

Get Unlimited Access Now

Transcript from the "Passing Arguments" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Bianca Gandolfo: So we passed in callbacks, we called them, but what if we want to add an argument?
>> Bianca Gandolfo: And so here's an example of us passing arguments to our function, so we have a couple math functions here one is called increment, one is called square. So increment just adds one.

[00:00:26] Square is going to multiply itself, it's going to multiply a number by itself. And here's our function called doMathSoIDontHaveTo. And what it does is it takes a number and then it takes a function. And what it does is it applies, it calls that function with that number. And so we implement this by just passing it, 5 so that's in.

[00:00:49] And then square, which is the function up here. We enter into the function body and it's just gonna return.
>> Bianca Gandolfo: It's gonna return square with five, which is what?
>> Bianca Gandolfo: What is this? This is a math test. 25.
>> Bianca Gandolfo: And what about this one?
>> Speaker 2: Five.
>> Bianca Gandolfo: Five.

[00:01:19] This is just an example of, we just call a function as if it's just stored in a variable name. There's no difference here.
>> Bianca Gandolfo: So how would we pass an argument here? To either isTrue or isFalse. What would we have to change to pass argument?
>> Bianca Gandolfo: Sean?
>> Sean: Put it in the function.

[00:01:56] [INAUDIBLE]
>> Bianca Gandolfo: Mm-hm, so the first thing we'd have to do like this, right? Assuming that we're getting our argument is being passed in from the parent function, we'd have to put it here. And then yeah, we just pass it just like this. Or we can just say even like this, we can say var arg = 0 or something.

[00:02:21] And then we can just have our arg inside the function. Or we could do it like that, either way. And then how would we call this function?
>> Speaker 4: Pull ifElse, and then condition, true or false and then your arguments gone.
>> Bianca Gandolfo: Mm-hm. And this would be a function, this would be a function, and then, pass a number or whatever.

[00:02:54] Maybe a string.
>> Bianca Gandolfo: Cool.
>> Speaker 5: Now, sorry. Cuz when we first did some of, when you did the A + B + C yesterday and it was a return A + B cuz it didn't care if you had three arguments.
>> Bianca Gandolfo: Mm-hm.
>> Speaker 5: Even if you don't specify it there in the function definition arg.

[00:03:23] It's just, so if you don't have that arg up top there in the function definition.
>> Bianca Gandolfo: Mm-hm.
>> Speaker 5: But you ran ifElse, and you had that asdf at the end, it's just not gonna do anything with that, like the C.
>> Bianca Gandolfo: Mm-hm, exactly. Exactly.
>> Speaker 6: Can you explain why we have to define the arg variable?

[00:03:44] Why couldn't we just when we actually called the isTrue function just pass in the parameter at the time of calling it?
>> Bianca Gandolfo: Mm, because like here you mean? So if we had this function here.
>> Bianca Gandolfo: [CROSSTALK] Like where? Like here?
>> Speaker 6: Right there, and then when you put the asdf in the exact, in the front of the function indication.

[00:04:11]
>> Bianca Gandolfo: Okay cuz this here, this is just the function definition. We're not invoking the function here, so the only way that we can give a parameter of value or cross an argument, is when you're actually calling the function and so, we're actually calling the function at this line.

[00:04:26] We're not calling the function here, we're just passing the definition. So, if we were to call the function, you have to have the two parentheses.
>> Speaker 6: So how does the Like if you had a quick event in jQuery, just thinking, can you pass the event variable in the call back function?

[00:04:46] What does that look like?
>> Bianca Gandolfo: That's being so when whenever that element is clicked jQuery is calling your call back function with that event object.
>> Bianca Gandolfo: So I have an example on the very top. So here, or sometimes you call it e. And then you've probably seen it as e.preventDefault, right, or something like that.

[00:05:22] Here you're only defining e as a parameter. So you're just saying, I'm just gonna call this e, but you can call it you know this, ASDF and do it that way, so whenever, say we have a click even on just the body here. We click on it and at that moment that function is called.

[00:05:43] So this function here is called at the moment of clicking and that's why we get data for the e. So the e object or the event object has data on where you actually clicked so it's being passed at the time of clicking. Mm-hm.
>> Speaker 5: The question is, how does r get passed to isTrue and isFalse if the parameter's only set on ifElse?

[00:06:08]
>> Bianca Gandolfo: Let's see. One more time.
>> Speaker 5: So how does that r get passed to isTrue and isFalse if it's only set on the ifElse?
>> Speaker 7: The function definition would have to take a parameter, and then use that parameter in its definition.
>> Bianca Gandolfo: Mm-hm.
>> Speaker 7: Or you'd have to use the argument's semi-array to get access to it.

[00:06:42]
>> Bianca Gandolfo: So, yeah, maybe this will answer the question. So, those are just example functions, but this function then is a function that takes that argument and then passes it to the function and we're just gonna console.log here, x. So x is a parameter. So this is where a parameter and arguments got a little bit tricky.

[00:07:12] So arg at this point is a parameter, x here is a parameter. asdf is a value that is an argument. All of these things are arguments in here. So true is an argument, this function definition is an argument, and when we pass asdf, arg now contains that value.

[00:07:39] arg here, is now you can imagine, it's ASDF and that's an argument and now isTrue, this x, here in isTrue, is then going to be ASDF and that's how you follow it through the function.
>> Speaker 5: Follow up question to that I think is, do you have to set that parameter in the function definition of isTrue and isFalse?

[00:08:17]
>> Bianca Gandolfo: No, you don't. So the other option is if you have no perimeters, or you don't know how many perimeters you can use, the argument's keyword, which is gonna print out an array of the arguments. So, if we only pause one argument and say ASTF it will have ASTF In an array like object.

[00:08:45] So we covered this yesterday in the function section, if you wanna review the argument's key word. It's pretty interesting.
>> Bianca Gandolfo: Cool.