SKU // 41

Function Parameters

Parameters are how a function receives information. They let one function handle many situations: the same greet function can greet anyone, because the name is passed in rather than written into the code.

// Parameters vs Arguments

These two words get mixed up constantly, so here they are side by side:

//                 parameters (placeholders)
function multiply(a, b) {
  return a * b;
}

//                   arguments (real values)
console.log(multiply(4, 5));
20

When called, a becomes 4 and b becomes 5, for that one run only.

// One Parameter, or Many

A function can have as many parameters as you need, separated by commas:

function toCelsius(fahrenheit) {
  return (5 / 9) * (fahrenheit - 32);
}

function fullName(firstName, lastName) {
  return firstName + " " + lastName;
}

console.log(toCelsius(77));
console.log(fullName("John", "Doe"));
25 John Doe

// Order Matters

Arguments are matched to parameters by position, not by name. The first argument goes into the first parameter, and so on:

function describe(name, age) {
  return `${name} is ${age}`;
}

console.log(describe("Sam", 16));
console.log(describe(16, "Sam"));    // oops
Sam is 16 16 is Sam

// JavaScript Does Not Check

Many languages make you declare what type each parameter is, and refuse to run if you pass the wrong thing. JavaScript does none of that:

So a wrong argument does not cause an error. It quietly gives a wrong answer:

function add(a, b) {
  return a + b;
}

console.log(add("2", 3));      // wrong type: joins instead of adding
console.log(add(2));           // too few: b is undefined
console.log(add(1, 2, 3));     // too many: the 3 is ignored
console.log(toCelsius());      // missing: fahrenheit is undefined
23 NaN 3 NaN
Watch out: missing arguments become undefined
Any parameter that does not get an argument is undefined. In math, that turns into NaN; in a string, it shows up as the word "undefined" ("Hello, undefined"). If you see either where you expected real data, count your arguments.

// Default Parameters

Since ES2015, a parameter can have a default value, written with =. It is used only when that argument is missing (or undefined):

function myFunction(x, y = 10) {
  return x + y;
}
console.log(myFunction(5));       // y uses its default
console.log(myFunction(5, 1));    // y is given

function price(amount, taxRate = 0.08) {
  return (amount * (1 + taxRate)).toFixed(2);
}
console.log(price(100));
console.log(price(100, 0.06));
15 6 108.00 106.00
Watch out: null does not trigger the default
A default is used for a missing argument or undefined, but not for null. With function greet(name = "friend"), calling greet(null) gives "Hello, null", and greet("") gives "Hello, ". Only greet() and greet(undefined) use "friend".
Tip
Put parameters that have defaults at the end of the list. That way callers can simply leave them off, instead of passing undefined to skip a slot.

// Parameters Are Local Copies (for Simple Values)

Parameters behave like local variables. Changing a parameter that holds a number, string, or boolean does not affect the variable that was passed in:

function addTen(n) {
  n = n + 10;
  return n;
}

let score = 5;
console.log(addTen(score));
console.log(score);             // unchanged
15 5

The function got a copy of the 5, not the variable score itself.

// But Arrays and Objects Are Shared

Arrays and objects work differently. The function receives a reference to the same array, not a copy, so changes made inside are visible outside:

function addBonus(list) {
  for (let i = 0; i < list.length; i++) {
    list[i] += 10;
  }
}

const scores = [70, 85, 90];
addBonus(scores);
console.log(scores);
[80, 95, 100]
Watch out
A function quietly changing an array or object you passed in is a common source of confusing bugs. If a function should not change its input, have it build and return a new array instead. The Arrays and Objects sections come back to this.

// Rest Parameters: Any Number of Arguments

Three dots before the last parameter collect all remaining arguments into an array:

function sumAll(...numbers) {
  let total = 0;
  for (const n of numbers) {
    total += n;
  }
  return total;
}

console.log(sumAll(1, 2));
console.log(sumAll(5, 10, 15, 20));
console.log(sumAll());
3 50 0

These are the same three dots as the spread operator (Math.min(...list)), doing the opposite job: spread splits an array apart into separate values; rest gathers separate values into an array. Function Arguments covers more.

// Quick Check

What does this log?

function area(w, h) {
  return w * h;
}
console.log(area(6));
Show answer
NaN. Only one argument was passed, so h is undefined, and 6 * undefined is NaN. JavaScript does not warn you.

With function hi(name = "friend") { return "Hi " + name; }, which call uses the default?

  1. hi("")
  2. hi(null)
  3. hi(undefined)
  4. hi(0)
Show answer
C. A default only kicks in for a missing argument or undefined. The others are real values (even though they are falsy), so they are used as-is: "Hi ", "Hi null", "Hi 0".

After this runs, what is level?

function levelUp(n) {
  n++;
}
let level = 1;
levelUp(level);
Show answer
1. The function changed its own local copy n, not level. To update it, return the new value and assign it: level = levelUp(level); with return n + 1; inside.

// Try It

  1. Click Open Playground. Call fullName with the arguments in both orders
  2. Add a default: power(base, exponent = 2). Call it with one argument and with two
  3. Call power() with null as the exponent. Explain the result
  4. Write average(...nums) using a rest parameter, and test it with 2, 5, and 0 numbers. What goes wrong with 0?
// Video Walkthroughs
Last updated: Oct 10, 2026, 12:52 AM ET