Please enable JavaScript to use CodeHS

Default Parameters in JavaScript

By Ryan Hart

Parameters are super helpful in JavaScript because they allow values to be passed into a function when the function is called. See the Basic Functions tutorial for more on how parameters work.

When you define a function with parameters, the parameters have a default value of undefined. Only when you call the function with arguments do the parameters override that default and take on the desired values. The example below prints out the value of a parameter for both cases – with and without an argument.

Setting the default parameter

Sometimes we want something other than undefined as the default for our parameters. For example, in the program above, we may want the function to print “Hello” by default if no arguments are passed in.

Setting different values as defaults for our parameters is easy to do, and it’s done in the function declaration statement. When listing your parameter, include an assignment statement for it!

Instead of

function greeting(message){
}
Plain text

Now it’s

function greeting(message = "Hello"){
}
Plain text

Below is the same program as before, but with the addition of our new default. Notice how it changes the output when there are no arguments in the call statement!

  • Try changing the default to different values.
  • What kind of values can the default be set to?

More than one parameter

In many cases, our functions have more than one parameter. How do we set defaults in this case? It’s the same – we just include an assignment statement for each parameter that needs a default.

function greeting(message = "Hello", name = "buddy"){
    println(message + " " + name);
}
Plain text

You can even have some parameters with no new defaults mixed in those that have them!

function greeting(punctuation, message = "Hello", name = "buddy"){
    println(message + " " + name + punctuation);
}
Plain text

While technically you can list the parameters in any order, the general best practice is to list those without defaults first. This reduces the risk of them being left as undefined.

Take a look at the updated example below to see how these functions work with different arguments sent in!

  • Again, try changing the values of the defaults.
  • Create your own call statements with varied number of arguments. Try to guess the output before you press run!

Example: Passing undefined

Take a look at what happens when we pass the value of undefined in as an argument for a parameter that has a custom default. What ends up being the output?

When you run the program, you’ll notice that when we pass the value of undefined as an argument, the parameter is assigned the default value assigned in the declaration. This technique is helpful when you want to pass an argument to a parameter later in the list, bypassing earlier ones.

Referencing other parameters

A function can even have parameters that are assigned the values of other parameters that occur earlier in the list. Remember though, these are default values, so any passed in arguments will overwrite them.

  • What happens if you reverse the order of the parameters in the declaration list?
  • Try writing your own call statements and predict the output before running.
  • Can you think of scenarios where this technique of referencing parameters would be helpful?

Your turn!

Now it’s your turn. Try making functions and assigning default values to your parameters. Here are a few ideas to try:

  • Write a function that multiplies four arguments together, where the default values for the parameters are all 1.
  • Write a function that calculate the slope between two (x, y) points, where the default values of the second coordinate is (0, 0). Hint: slope = (y1 - y2) / (x2 - x1)
  • Write a function that prints a greeting in a language specified by the argument, where the default language is Spanish. Hint: you can use an if/else structure to identify language and print desired greeting!