Function Parameters

Pass data into functions. Default values, rest params, and more.

Parameters & Arguments

Parameters are placeholders. Arguments are the actual values.

// 'a' and 'b' are PARAMETERS (in definition)
function add(a, b) {
  return a + b;
}

// 5 and 3 are ARGUMENTS (in call)
add(5, 3); // 8

add(5, 3) → 8

// Missing arguments become undefined:
function greet(name, greeting) {
  console.log(greeting + ", " + name);
}
greet("Alice"); // "undefined, Alice"

// Extra arguments are ignored:
greet("Alice", "Hello", "extra"); // "Hello, Alice"

Default Values

Provide fallback values for missing arguments.

function greet(name = "World", greeting = "Hello") {
  return `${greeting}, ${name}!`;
}

greet();              // "Hello, World!"
greet("Alice");       // "Hello, Alice!"
greet("Alice", "Hi"); // "Hi, Alice!"
// Default only applies when undefined:
function test(x = 10) {
  return x;
}
test(undefined); // 10 — uses default
test(null);      // null — NOT undefined
test(0);         // 0 — NOT undefined
test("");        // "" — NOT undefined

Defaults ONLY trigger for undefined, not for null, 0, or empty string.

1 / 2

Rest Parameters

Collect any number of arguments into an array.

// ...rest collects remaining arguments:
function sum(...numbers) {
  let total = 0;
  for (const n of numbers) {
    total += n;
  }
  return total;
}

sum(1, 2, 3);     // 6
sum(1, 2, 3, 4, 5); // 15
sum();             // 0
// Rest must be last parameter:
function tag(name, ...attributes) {
  console.log(name);       // "div"
  console.log(attributes); // ["id", "class"]
}
tag("div", "id", "class");

// ✗ This is invalid:
// function bad(...rest, last) {} // SyntaxError

arguments (old)

Array-like, not a real array. No .map(), .filter(). Avoid in modern code.

...rest (modern)

A real Array. Has all array methods. Clear and explicit.

Pass by Value vs Reference

Primitives copy. Objects share.

// Primitives: pass by value (copy)
function double(x) {
  x = x * 2;
  console.log("inside:", x); // 20
}
let num = 10;
double(num);
console.log("outside:", num); // 10 ← unchanged!

Primitives (numbers, strings, booleans) are copied. Changes inside don't affect the original.

1 / 3

FAQ

Common questions about parameters.