FAQ: Functions - Default Parameters

Grand. Thanks. Helped a lot!!!

Why would you use name = ‘stranger’ as a parameter and not name || ‘stranger’? I thought = always assigned a value. Just trying to understand the use of these operators.

The default parameter is akin to an OR, already. Use name supplied in the argument, OR if undefined (not supplied) use 'stranger'.

For a function that has multiple default values, is there a way to specify which variables are being replaced when calling the function with less parameters than possible?
i.e.: on the lesson, we have the header:
function makeShoppingList(item1=‘milk’, item2=‘bread’, item3=‘eggs’)
is there a way to call it with 2 parameters so that it considers it as parameters 2 and 3? Or 1 and 3?

1 Like

In the shopping list example if I just type makeShoppingList(‘milk’) it gives me all 3 reminders. Can someone explain (in simple terms) why it gives me all three reminders?

Remember to buy milk
Remember to buy bread
Remember to buy eggs

Example:
function makeShoppingList(item1 = ‘milk’, item2 = ‘bread’, item3 = ‘eggs’) {
console.log(Remember to buy ${item1});
console.log(Remember to buy ${item2});
console.log(Remember to buy ${item3});
}

makeShoppingList(‘milk’)

I am confused here with the shopping list.

function makeShoppingList(item1 = ‘milk’, item2 = ‘bread’, item3 = ‘eggs’){
console.log(Remember to buy ${item1});
console.log(Remember to buy ${item2});
console.log(Remember to buy ${item3});
}

makeShoppingList(‘kakao’, null ,‘orange’);
//output: remember to buy kakao, remember to buy null, remember to buy orange.

What if i want to keep the position 2 empty for instance?? I want that position one is kakao, position two to the be empty - therefore display default parameter “bread” and position 3 to be ‘orange’.

How to make one of the parameters empty so that the default will show up in correct order?

Change null to undefined. null is a defined value.

1 Like

thanks a lot! it works :slight_smile:

1 Like

How may i change the second, or third default parameter? Is is at all possible? At least in python you may call the function like this;
makeShoppingList(item2 = “carrots”)
and then only item2 would change in the funtion, how would you approach this in JS?

(the function would then print:
Remember to buy milk
Remember to buy carrots
Remember to buy eggs)

Thank you

I can’t understand default parameter totally?

Thanks

function greeting (name = ‘stranger’) {
console.log(Hello, ${name}!)
}

greeting(‘Nick’) // Output: Hello, Nick!
greeting() // Output: Hello, stranger!

I’m a bit lost here and assuming I’m posting in the right area with this, my first post. (Please let me know if that assumption is correct!)

My question: I understand how the above code works. What I don’t understand is what order this all happens in. I feel it would help me to know the order so that I can absorb things " deep into my bones ". Can anyone help? Thanks!

Yes this is the right place for questions.

When the function is called it looks for an argument.

greeting(“Nick”); (This has an argument of “Nick”)
greeting(); (This has no argument)

If there is an argument in the function call, the argument is used in the place of the name parameter. If no argument is found the default parameter “Nick” is used. Once that’s all sorted out the code block is executed.

I hope this helps.

I thought the default parameter is , "stranger".

Thanks, guys. I’m trying to get the sequence of things as if I’m the browser running the code. In the following script, is my assessment of the events chronologically correct ?

function multiplyByNineFifths(number) {
return number * (9/5) ;
}
function getFarenheit (celsius) {
return multiplyByNineFifths (celsius) + 32 ;
}
getFarenheit (15) ;

Is this the sequence?? …

  1. The browser first looks at function multiplyByNineFifths and passes over it because it has nothing assigned to it just yet. Instead, it jumps down to getFarenheit(15) and “calls” MultiplyByNineFifths and passes 15 as an “argument”.

  2. MultiplyByNineFifths(number)…number is now replaced by 15 as an argument

  3. The code block inside MultiplyByNineFifths multiplies 15 by 9/5 (which is 27)

  4. 27 is returned(?) back to the function call getFarenheit(15) to now become getFarenheit(27) ?

  5. getFarenheit then adds 32 to 27 (59) to become getFarenheit(59)

I appreciate your input very much!

Yes. Of course. Sorry about that. It should had read.

If no argument is found the default parameter “stranger” is used.

1 Like

When this code is executed I believe order should look like this:

  1. The functions are put into memory during the compile phase.

  2. The browser will execute the code from top to bottom.

  3. It takes note of the placement of multiplyByNineFiths function but moves past this function because it has not yet been called.

  4. It takes note of the placement of getFarenheit function but moves past this function because it has not yet been called.

  5. getFarenheit(15); calls the function getFarenheit which would insert the argument 15 into the parameter celsius. The way I think about it is it looks like this.

getFarenheit(15){
return multiplyByNineFifths(15) + 32;
}

  1. This will call the function multiplyByNineFifths with the argument of 15 in place of the parameter numer. The function getFarenheit would pause there to run multiplyByNineFifths. The rest of the function would wait for multiplyByNineFifths to be ran before continuing. This is called opening a stack. (Looking into javascript stacks would help to understand this process.)

  2. multiplyByNineFifths would execute. (This would be the top of the stack. It would work back down the stack from here.)

multiplyByNineFifths(15){
return 15 * (9/5);
}

  1. multiplyByNineFifths would return 27 back into the previous stack which would look something like this.

return 27 + 32;

  1. The function call that we began executing (getFarenheit(15)) would be replaced by the previous return of 59;

You could also have saved the function call in a variable if that helps make sense of 59 being returned.

example:
function multiplyByNineFifths(number) {
return number * (9/5) ;
}
function getFarenheit (celsius) {
return multiplyByNineFifths (celsius) + 32 ;
}
let tempInFarenheit = getFarenheit (15) ;

In this example step 9 would return 59 from the function getFarenheit saving 59 into the variable tempInFarenheit.

This may have been long winded but I was trying to express how I visualize the steps executing chronologically as clearly as possible. Hope it helps.

1 Like

Thank you, arendestes! This was exactly what I was looking for. It was downright fascinating to see how you, as an experienced programmer, analyzed the sequential steps in this script/program. It really helped me understand the inner mechanism of Javascript. It especially helped me see that I have a looong way to go…lol…(but I’m also really having fun on the journey.) :slight_smile:

1 Like

I am 3 days stuck on this and this is what makes more sense to me

function makeShoppingList(parameter = ‘Default Value’,item1 = ‘milk’, item2 = ‘bread’, item3 = ‘eggs’) {
console.log(Remember to buy ${item1});
console.log(Remember to buy ${item2});
console.log(Remember to buy ${item3});
}

makeShoppingList()

But the only thing I got was ’ Did you provide default values to the parameters in makeShoppingList() ?’ =/

Im done! don’t know what to do =/

Should that even be in the parameter list?

I solved already then I saw how wrong I was… =/

1 Like