Skip to main content
🟨 JavaScript Foundations

What's a Function? πŸ› οΈ

Next you'll write functions β€” little machines that take inputs and hand back an output. Let's read a few first so the shape feels familiar.

A function has a name, takes inputs in parentheses, and hands a value back with the word return:

function double(n) {
  return n * 2;
}
double(5); // 10

The value in the parentheses (5) becomes n inside the function. Without return, a function hands back undefined β€” nothing useful.

Read each snippet below and predict the answer. πŸ‘‡

What does double(10) hand back?

function double(n) {
  return n * 2;
}

What does greet("Sam") hand back?

function greet(name) {
  return "Hi " + name;
}

What does this function hand back?

function mystery(n) {
  n + 1;
}