Skip to content
Codewilds

Lesson 9 of 34 · Functions

JavaScript Functions: Parameters and Return Values

easy10 XP3 min read

You've already filled in a few functions. Now it's time to understand them properly. Functions are the most important building block in JavaScript: they let you name a piece of logic, reuse it anywhere and test it on its own.

In this lesson you'll learn how to declare functions, pass them data through parameters, send results back with return, and make parameters optional with default values.

Declaring and calling a function

A function declaration starts with the function keyword, a name, a list of parameters in parentheses and a body in curly braces:

function greet(name) {
  return `Hello, ${name}!`;
}

console.log(greet("Ada"));   // Hello, Ada!
console.log(greet("Linus")); // Hello, Linus!

Declaring a function doesn't run it. The code inside only runs when you call the function by writing its name followed by parentheses: greet("Ada").

Parameters and arguments

Parameters are the variable names listed in the declaration (name). Arguments are the actual values you pass in a call ("Ada"). A function can take as many parameters as it needs:

function damage(attack, defense) {
  return attack - defense;
}

console.log(damage(50, 20)); // 30

Arguments are matched to parameters by position. If you pass fewer arguments than there are parameters, the missing ones are undefined; extra arguments are ignored.

Returning a value

return does two things: it ends the function immediately, and it hands a value back to the caller. That value can be stored, printed or passed to another function:

function square(n) {
  return n * n;
}

const area = square(4);
console.log(area);              // 16
console.log(square(square(2))); // 16

A function without a return statement gives back undefined. That's the source of one of the most common beginner bugs: printing a value is not the same as returning it.

function addLoud(a, b) {
  console.log(a + b); // shows 5 on the screen…
}

const result = addLoud(2, 3);
console.log(result); // …but the function returned undefined

Code after a return never runs, so return as the last step, or use early returns to handle special cases first.

Default parameter values

Sometimes a parameter is optional. Give it a default with =, and the default is used whenever the argument is missing (that is, undefined):

function heal(hp, amount = 10) {
  return hp + amount;
}

console.log(heal(50));     // 60 (amount defaults to 10)
console.log(heal(50, 25)); // 75

Defaults only kick in for undefined. Passing null or 0 counts as a real value. To skip an earlier parameter while setting a later one, pass undefined explicitly: f(undefined, "value").

Function declarations are hoisted

JavaScript processes function declarations before running your code, so you can call a declared function above the place where it's written. It's still clearer to define helpers before you use them.

Common mistakes

  • console.log instead of return. If a test says your function returned undefined, check that you actually return the result.
  • Forgetting the parentheses. greet refers to the function itself, while greet() calls it.
  • Mixing up argument order. damage(20, 50) is not damage(50, 20). Arguments are matched by position, not by name.

Assignment

Write two functions:

  1. calculateDamage(attack, defense, multiplier) returns (attack - defense) * multiplier, but never less than 0. The multiplier is optional and defaults to 1.
    • calculateDamage(50, 20) → 30
    • calculateDamage(50, 20, 2) → 60
    • calculateDamage(10, 30) → 0
  2. createGreeting(name, greeting) returns "<greeting>, <name>!". Both parameters are optional: name defaults to "traveler" and greeting defaults to "Hello".
    • createGreeting("Ada", "Welcome") → "Welcome, Ada!"
    • createGreeting() → "Hello, traveler!"

Premium lesson

Unlock the full Learn JavaScript course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Functions and methods in other languages

Need a quick reminder? The JavaScript cheat sheet has every topic of Learn JavaScript on one page.

Lesson last updated on .

Write code ↓
JavaScript+10 XP

Lesson 9 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn JavaScript course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.