FAQ: Functions - Default Parameters

This community-built FAQ covers the “Default Parameters” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Default Parameters

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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’);
makeShoppingList(‘bread’);
makeShoppingList(‘eggs’);

Output:Remember to buy milk
Remember to buy bread
Remember to buy eggs
Remember to buy bread
Remember to buy bread
Remember to buy eggs
Remember to buy eggs
Remember to buy bread
Remember to buy eggs

Can explain why the output like that?

Thanks!

1 Like

Because when you enter in only one argument, it replaces only the first default parameter, not the other two.

2 Likes

so how can you get that the output should be
Remember to buy milk
Remember to buy bread
Remember to buy eggs
without putting a default parameter?

function makeShoppingList(item1, item2, item3){
  console.log( `Remember to buy ${item1}` );
  console.log( `Remember to buy ${item2}` );
  console.log( `Remember to buy ${item3}` );
}
makeShoppingList('milk', 'bread', 'eggs')
5 Likes

And the semicolon at the end of course. ^^

1 Like

Is there another way or method to assign default values?

1 Like

The ES6 method is,

function foo(a, b, c=1) {

}

The ES5 method is,

function bar(a, b, c) {
   if (c === undefined) {
        c = 1;
    }
}
3 Likes

Is there a reason why a template literal was used in the example? I copied the code from the example and got rid of the template literal and ended up with the same result.

This is what I typed:

function greeting (name = ‘Stranger’) {
console.log("Hello " + name + “!”);
}

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

1 Like

The whole intent of template literals is to simplify string interpolation and reduce quote errors.

console.log(`Hello ${name}!`);

No concatenation, no quoting string characters, and only one set of backtick quotes. All nicely wrapped up.

Another neat feature is that since the template takes an expression, that expression may be itself a template literal.

3 Likes

Hi,

I am interested in how to change for the second item but to keep the first and the third default?

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('beer');

This will return by changing the first one and keep the next two default.

Thanks in advance.

4 Likes

We can’t since non-defaults must appear first in the arguments list.

This will change item1 to ‘beer’, not item2.

If you wish to be able to leave ‘milk’ and ‘eggs’ then make item1 = ‘bread’. This will permit calling with a single argument.

2 Likes

If you pass undefined as an argument, it will use the default. For 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(undefined, 'beer');

Outputs:
Remember to buy milk
Remember to buy beer
Remember to buy eggs

23 Likes

Right you are. I stand corrected. Memory is not what it once was.

4 Likes

I have a question about default parameters. In the example code within the lesson it state’s that.

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});
}

If I’m happy with the first two default values, but I want to change the third one, how do I go about doing this.

I’ve tried the following…

makeShoppingList(,‘Tuna); //Which results in an Unexpected Token error
makeShoppingList( , ,‘Tuna’); //Which results in an Unexpected Token error
makeShoppingList(’ ‘,’ ',‘Tuna’) //Which results in two black items and Tuna
makeShoppingList(‘Tuna’); //Which replaces the first item
makeShoppingList(false,false,‘Tuna’) //Which prints false, false, Tuna.

I’m really confused how you would do this, hope my question makes sense :slight_smile:

Cheers in advanced guys,

Luke.

Sorry answer is in the post above!

basically we can’t change a function for now, but you never know what JS will bring us in next chapter)

I have 3 parameters for function with default parameters
calling bio function without “name” parameter,

function bio (name = "hidden", age=21, gender = "male"){

console.log(`Your name is ${name}, your age is ${age}, and you are ${gender} `);

}

bio("",33, "male");

expected output:
Your name is hidden, your age is 33, and you are male;
instead I’m getting errors
I have 3 version, how to call it, and none is working

bio(,33, "male");

bio("",33, "male");

bio(``,33, "male");

Is it working with ONLY ONE parameter?


function bio (name = "unknown"){

console.log(`Your name is ${name}`);

}

bio();
//your name is unknown

Try,

bio(undefined, 33)

Parameters on the right can be allowed to default, but parameters on the left need their arguments set to undefined if the default value is to apply.

Above, will render as,

Your name is hidden, your age is 33, and you are male
2 Likes

wow big thanks, was googling a lot, looks like nobody want’s to skip parameters.thanks one more time

1 Like