What a function is
A function is a named piece of code you can run whenever you need it. You give it inputs, it does some work, and it gives back a result.
function add(a, b) {
return a + b;
}
add(2, 3); // 5function starts the definition, add is its name, a and b are its inputs, and the code between the braces runs each time you call add(...).
Parameters and arguments
The names in the definition, a and b, are parameters. The values you pass when you call it, 2 and 3, are arguments. Inside the function, each parameter holds the argument in the same position.
Return gives the value back
return ends the function and hands a value back to whoever called it. Without it, the caller gets nothing useful. MDN puts it precisely: if a function's execution does not end at a return statement, the return value is undefined.
console.log is not return
This is the most common first mistake:
function add(a, b) {
console.log(a + b); // prints 5
}
const total = add(2, 3);
total; // undefinedconsole.log prints a value so a person can see it. It does not give the value back to your code. The tests on this site call your function and check what it returns, so printing alone fails with "got undefined". Printing is still useful while you work out what is going on; just return the answer as well.
Returning early
return stops the function at once, which lets you handle a special case first and keep the main logic flat:
function factorial(n) {
if (n === 0) return 1;
let total = 1;
for (let i = 2; i <= n; i++) total *= i;
return total;
}Other ways to write a function
You will see functions written in a few shapes. They behave the same for the exercises on this site:
// a function declaration
function double(n) {
return n * 2;
}
// a function expression stored in a constant
const triple = function (n) {
return n * 3;
};
// an arrow function: with one expression, the value is returned
const square = (n) => n * n;The arrow form has one trap: with curly braces, you need return again. (n) => { n * n } returns undefined, because the braces start a block of statements.
Keep functions small
A good function does one job and has a name that says what it returns: isEven, largest, countVowels. If you find yourself writing "and" when you describe a function, it may be two functions. Small functions are easier to test, because you can call each one with a few inputs and check the answers, which is exactly what the exercises do.
Variables inside a function
Variables declared inside a function with let or const exist only while that call runs. Each call starts fresh, so a counter set to 0 at the top of a function is 0 again on the next call. If you need a result after the function ends, return it; do not rely on a variable from inside.
Common mistakes
- No return at all. The function runs but gives back
undefined. - Return inside a loop by accident. The function stops on the first item. Put the return after the loop.
- Renaming the function. Tests call it by name, so
function plusinstead offunction addmeans the test cannot find it. - Changing the input. Methods such as
reverse()change an array in place. Copy first if the caller still needs the original.
Exercises 1 and 2 in the playground practise exactly this.
Key points
- A function takes inputs (parameters), does some work and gives back one value with return.
- Without a return, the caller gets undefined.
- console.log shows a value to you; return gives it to your code.
- An arrow function with braces needs return too.
- Variables inside a function start fresh on every call.
- Small functions with clear names are easier to test and to fix. Try writing one that uses another: for example, a function that returns the average of a list by calling a separate sum function. Combining small, tested functions is how bigger programs are built.
Try it on a real exercise
Write a function, run the tests, get a hint if it fails.
Sources checked 4 December 2026.