Home › Learn › JavaScript › Making decisions

Functions

Lesson 10 of 40. Package code under a name with inputs and a result.

function name(parameters) { ... } defines a function; name(arguments) runs it. return value hands a result back and ends the function. Without a return, the result is undefined. Function declarations are hoisted, so you can call one above the line that defines it.

Returning is not printing. console.log shows a value to a person; return gives it to the code that called the function, which can store, compare, or pass it on. The checks call your functions and look at the returned values, so return, do not print.

function circleArea(radius) {
  return Math.PI * radius ** 2;
}
const big = circleArea(10);
console.log(big > 300);

Your task

Write rectangleArea(width, height) returning the area, isEven(n) returning true for even numbers and false otherwise, and shippingCost(subtotal) returning 0 when the subtotal is 50 or more and 5.99 otherwise.

Part of the final project. shippingCost decides the shipping line of the receipt: free from 50 dollars, 5.99 below. It is saved in this browser and loaded into Project: checkout when you get there.

Hint 1

return n % 2 === 0; is already a boolean.

Hint 2

if (subtotal >= 50) return 0; return 5.99;

Show the solution
function rectangleArea(width, height) {
  return width * height;
}

function isEven(n) {
  return n % 2 === 0;
}

function shippingCost(subtotal) {
  if (subtotal >= 50) return 0;
  return 5.99;
}

    Runs in an isolated web worker in this tab; nothing is sent anywhere.