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.
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));
When called, a becomes 4 and b becomes 5, for that one run only.
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"));
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
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
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.
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));
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".
undefined to skip a slot.
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
The function got a copy of the 5, not the variable score itself.
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);
return a new array instead. The Arrays and Objects sections come back to this.
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());
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.
What does this log?
function area(w, h) {
return w * h;
}
console.log(area(6));
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?
hi("")hi(null)hi(undefined)hi(0)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);
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.fullName with the arguments in both orderspower(base, exponent = 2). Call it with one argument and with twopower() with null as the exponent. Explain the resultaverage(...nums) using a rest parameter, and test it with 2, 5, and 0 numbers. What goes wrong with 0?