FAQ: Functions - Default Parameters


#1

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!


#2

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!


#3

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


#4

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?


#5
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')

#6

And the semicolon at the end of course. ^^


#7

Is there another way or method to assign default values?


#8

The ES6 method is,

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

}

The ES5 method is,

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

#9

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!


#10

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.