Skip to the content

Default values

JavaScript · Unit 4: Functions

A parameter can come with a default value: rate = 20.

If you don't pass it, it uses that value. If you pass it, it uses yours.

function tip(check, rate = 20) {
  return check * rate / 100;
}

console.log(tip(200));
console.log(tip(200, 25));

Prints

40
50

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Predict the output

    What does this code print?

  3. 3. Complete the code

    Fill in the blank so the quantity is 1 when nobody says it.

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Fill in the blank to order the tea with 2 sugars, without changing the milk. It should print 1 2.

  6. 6. Find the bug

    This function should subtract an optional discount from the price, so total(100) should give 100. Which line has the error?

  7. 7. Multiple choice

    You want to be able to call pay(200) and have the tip be 20. Which definition works?

  8. 8. Put the lines in order

    Put the program in order. It should print "Ava in row A".

  9. 9. Find the case that fails

    tip(check, rate) should hand back the check with the tip added. If nobody says the rate, it is 20 %. Which call makes it fail?

  10. 10. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →