When the JavaScript interpreter gets to the function called bark, it doesn’t run the code that’s in the function. Instead it just remembers that there is a function there called bark.
function bark(name, weight) {
...
}
Whenever you use the bark function, which is the word bark followed by information in () parenthesis, it then passes the arguments that you give it to the function.
For example with bark("rover", 23) the first argument is the string "rover" and the second argument is the number 23
When those arguments are passed to the bark function, they are received inside of the function as parameters in the same order that they’re given. With the bark function, those parameters are called name and weight
The argument "rover" is received by the bark function as parameter called name inside of that function. The other argument 23 is received by the bark function as a parameter called weight.
When the bark("rover", 23) command takes place, that is when JavaScript goes to the bark function to run the code that’s inside of there. When that function finishes, code execution returns to whatever is after the `bark(“rover”, 23) command.
The following tweet from Wes Bos is the best way I’ve seen to put many of these terms into context:
i am confused how the arguments become parameters which then are applied to the body’s weight and name… how does the body’s name and weight know i want “rover” and 23? where do i put that?
how do i code the arguments name to be “rover” and weight to be 23 then have that applied to the name and weight in the body…
or how does function bark(name, weight) become the code for “rover” and 23?
how about this… where do i enter the call and how do the parameters fill?
i get how it should look ultimately, just not how it gets there
i want you to know i sincerely appreciate your time and trouble!
boy if this is simple and i just don’t get it…
What you have there is called a function definition. It defines a function, but none of the code gets run until you invoke the function.
You call, or invoke, a function by using the name of the function followed by opening and closing parenthesis.
bark()
When you invoke the bark function with no arguments, the bark function will then occur with undefined as its parameters.
When you invoke the bark function with some arguments, JavaScript passes those arguments to the function.
bark("Rover", 23);
The function then receives those arguments as its own parameters. A handy rule of thumb is that arguments are given, while parameters are received.
Usually it is done after the function has been defined. It can be done before the function definition too, but there can be some edge-cases that just make it easier and more reliable when done afterwards.
The parameters default to all being undefined. When you call the function with some number of arguments, those are passed to the function in the same order.
If you don’t use enough arguments, the remaining parameters will be undefined. If you use too many arguments, the extra arguments are just ignored and don’t affect the function parameters.