DMarlon Studio

Lesson 12 · 12 of 20

Parameters, defaults and return · JavaScript

More on JavaScript functions: parameters with default values, return to exit early, undefined, rest parameters and returning an object.

JavaScript · Parameters, defaults and return in DMarlon Studio: the example's code and its output in the terminal

A parameter can have a default value with =: orderCoffee(size = "medium", sugar = 1) uses those values when the argument is missing. That is why orderCoffee("small") comes with one sugar and orderCoffee() is a medium coffee.

`return` ends the function right there. finalPrice first checks whether the price is negative and, if so, returns the warning without ever working out the discount. Exiting early like this saves nesting lots of ifs.

A function without return gives back `undefined`: showScore prints the score, but what it returns is of no use. Printing a value and returning it are two different things.

Rest parameters, with three dots (...grades), collect every argument passed into an array, whether there are three or none. average adds them up with for...of and, if there are none, returns 0 instead of dividing by zero.

A function returns a single value, but that value can be an object holding several pieces of data, as stats does. In an arrow function the object goes in brackets, ({ ... }), so the braces are not mistaken for the body.

Code · main.js

// Default values are used when an argument is missing.function orderCoffee(size = "medium", sugar = 1) {  return `${size} coffee with ${sugar} sugar`;}console.log(orderCoffee("large", 2));console.log(orderCoffee("small"));console.log(orderCoffee());// return ends the function at once: useful to stop early.function finalPrice(price, discountPercent = 0) {  if (price < 0) return "invalid price";  const discount = price * discountPercent / 100;  return price - discount;}console.log(finalPrice(80, 25), "|", finalPrice(80), "|", finalPrice(-5, 10));// Without return, a function gives back undefined.function showScore(points) {  console.log("Score:", points);}console.log("showScore returned:", showScore(120));// Rest parameters (...) collect any number of arguments in an array.function average(...grades) {  let sum = 0;  for (const g of grades) sum += g;  return grades.length > 0 ? sum / grades.length : 0;}console.log("Average:", average(6, 7.5, 9), "| Of nothing:", average());// To give back several values, return an object.const stats = (a, b) => ({ total: a + b, difference: a - b });console.log(stats(30, 12));

Output · node main.js

large coffee with 2 sugar
small coffee with 1 sugar
medium coffee with 1 sugar
60 | 80 | invalid price
Score: 120
showScore returned: undefined
Average: 7.5 | Of nothing: 0
{ total: 42, difference: 18 }

Key points

  • param = value sets a default value.
  • return leaves the function at once.
  • Without return, a function gives back undefined.
  • ...args collects any number of arguments in an array.
  • To return several values, return an object.

Exercise

Write ticketPrice(age, isStudent = false) that returns 0 for children under 4, 6 euros for students and 9 for everyone else, and total(...prices) that adds up every price it receives.