Foundation / JavaScript

JavaScript Functions, Conditions & Return Values

Use values, types, expressions, conditions, and functions to turn requirements into dependable behavior.

What you’ll learn

  • Choose const and let intentionally
  • Write clear conditions
  • Design focused functions

Before you start

You’ll need a modern browser, a basic HTML page, and a way to inspect the browser console. Practice with sample data. The interactive guide has its own activities and browser-saved progress.

Understand the idea

Values have types that affect operations. Inspect unexpected values with typeof and explicit logging instead of guessing.

Use const by default and let when reassignment represents meaningful state. Avoid hidden globals.

A focused function has a clear input, one responsibility, and a predictable return value. Separate calculation from display.

Follow the example

function shippingCost(total, member) {
  if (member || total >= 50) return 0;
  return 7.5;
}
console.log(shippingCost(42, false));

A function becomes easier to reuse when it calculates a result instead of updating the page directly. Keep the shipping rule in one function, test the boundary, and let the interface decide how to show the returned cost.

Try it yourself

Write a function that classifies a score as needs practice, developing, or proficient. Test boundary values before connecting it to a page.

Before you run the code, write down the result you expect. Change one input, test a boundary case, and explain why the output changes.

Practice this lesson in the full guide ↗

Common mistakes & troubleshooting

  • Try totals of 49.99 and 50 with membership disabled. The rule changes exactly at 50.
  • Check the value type before comparison. A value from a form input is a string until you convert it.
  • If your function returns undefined, check that every intended branch has a return statement.

Keep learning

Use your explanation to make a small feature of your own. Review the behavior before connecting it to real user data or a production application.