Skip to main content

Parameters

Parameters are named variables, passed into a function, and are used to import the arguments into functions.

Effectively, arguments are realized parameters through a function call.

An example:

Here are some key differences between parameters and arguments:

  • Function parameters are the names listed in the function's definitions

    • Function arguments are the actual values passed to the function.
  • Arguments are set to the values of the parameters supplied.

  • An arguments object exists but there is no equivalent for parameters.

info

The fundamental, basic idea of parameters are that they are unrealized arguments.

Arguments can't exist without parameters.

Parameters have two versions but we will only discuss one, as the second is not good practice:

  • Input parameters (pass values into functions)

Default Parameters

What happens if a user doesn't fill all parameters? What if you want it to be variable in size?

That is when default parameters come into play:

  • In the function definition, just add =value as a suffix to a parameter and you have a default parameter!

  • This means that if none is provided, the parameter will be set to value from above.

Here's an example:


Rest Parameters

What's the difference between a default parameter and a rest parameter?

  • Default parameters allow named parameters to be initialized with default values

    • Used when certain parameters are optional (so if they don't put one in it becomes the default value)
  • Rest parameters allow an indefinite, probably unnamed, amount of arguments as an Array.

    • It creates certain flexibility in function definitions.

The following is an example of the differences:

As previously mentioned, the rest parameter syntax allows a function to accept an indefinite number of arguments as an Array

It also allows quite a bit of flexibility in function definitions. Here is another example:

 

One can also mix normal and default parameters with rest parameters, for the most flexible function definitions:

tip

Only one rest parameter is allowed in a function, and it must be the last parameter in the definition.

Also, one can access the length of restParams easily: {restParamName}.length;