Essential JavaScript

Arguments

If functions are like recipes, then arguments are like ingredients.

Functions can take in values to work with, similar to how recipes call for different ingredients.

There are two functions in the editor:

  • returnNumber
  • addNumbers

The returnNumbers function takes a single argument named num. You can see it between the parentheses.

The addNumbers function takes two arguments: num1 and num2. Multiple arguments are separated by commas.

Changing Arguments

If you change the ingredients in a recipe, you change the result. The same thing happens when you change the arguments to a function.

Try calling returnNumber with different numbers:

  • returnNumber(3)
  • returnNumber(11)

Notice that the function itself doesn't change, but when we change the arguments passed into the function, we change the result of the function.

Multiple Arguments

Now try calling the second function with different arguments:

  • addNumbers(1, 2)
  • addNumbers(17, 5)

The value of num1 and num2 become whatever values we pass into the function.

Argument Names

Once we name an argument in the function parentheses, we can use that name to work with the argument inside the function.

Arguments can have almost any name. We can't use special JavaScript keywords like function, and argument names can't start with a number.

We should give arguments names that describe what the argument is or does. If an argument is a number, num or number are good names. If the argument is a specific type of number like a person's age or the number of apples we have, we should be more specific. Good names for those arguments could be personAge and applesCount.


Learning Goals

Code Editor