lecture 5 of 150 completed

Functions

A test is a function and so is every helper. A helper that forgets return hands back undefined.

What you'll learn

  • What parameters, arguments and return are
  • What a function gives back when it has no return
  • How to give a parameter a default value

Shelf charges a fee for each day a book is late. A test needs the expected fee, so someone writes a helper for it. Every test that uses the helper fails.

function lateFee(daysLate) {
  daysLate * 0.5;
}

const fee = lateFee(4);
console.log(fee);
console.log(fee === 2);
undefined
false

A function is a named block of code that you can run whenever you need it. Let's go through the parts.

  1. function lateFee(daysLate) creates a function called lateFee. daysLate is a parameter, which is a name for a value the function will receive.
  2. The curly brackets hold the body. The body works out daysLate * 0.5 and then does nothing with the result.
  3. lateFee(4) calls the function, which means it runs the body. The 4 is an argument, the real value for the parameter.
  4. The call gives back undefined, the value JavaScript uses for "no value".

A function gives a value back only when it says return. Without return, every call gives undefined. The language guarantees that, and it raises no error.

function lateFee(daysLate) {
  return daysLate * 0.5;
}

console.log(lateFee(4));
console.log(lateFee(4) === 2);
2
true

return also ends the function

When JavaScript reaches a return, it leaves the function at once. Lines after it do not run.

function describeLoan(title, daysLate) {
  if (daysLate === 0) {
    return title + " is on time";
  }
  return title + " is " + daysLate + " days late";
}

console.log(describeLoan("Dune", 0));
console.log(describeLoan("Emma", 3));
console.log(describeLoan("Ulysses"));
Dune is on time
Emma is 3 days late
Ulysses is undefined days late

The first call returns inside the if. The second skips the if and reaches the last line.

Look at the third call. It passes one argument to a function with two parameters. JavaScript does not complain. The missing parameter holds undefined, and the word ends up in the text.

Give a parameter a default

A default value is used when the caller leaves an argument out.

function describeLoan(title, daysLate = 0) {
  if (daysLate === 0) {
    return title + " is on time";
  }
  return title + " is " + daysLate + " days late";
}

console.log(describeLoan("Ulysses"));
Ulysses is on time

Why this matters in a test suite

Helpers are shared. One helper builds a user, another logs in, another works out an expected total. When one of them returns undefined, every test that calls it fails, and none of the failures points at the helper. Teams often lose an afternoon reading test after test before someone opens the helper file.

When many tests fail on the same morning, check what they share before you read each one.

Remember this

A function gives back what it returns, and nothing else. No return means undefined. A missing argument is also undefined, unless the parameter has a default.

Reference: Functions in the MDN JavaScript Guide.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.