bhylu.com

Advertisements

JS Best Practices

Keep functions focused and validate values at the boundary. Returning data makes a calculation easier to reuse and test.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

function bookingTotal(price, count) {
  if (!Number.isFinite(price)) throw new TypeError("Invalid price");
  return price * count;
}
console.log(bookingTotal(6, 2));

What happens

The function returns 12 for a valid numeric price.