Arguments are the actual values you hand to a function when you call it. The Parameters lesson covered the receiving end. This lesson covers the sending end: how arguments are matched up, what happens when the count does not match, how to handle any number of them, and how to pass a whole list at once.
function multiply(a, b) { // a and b are PARAMETERS
return a * b;
}
let result = multiply(4, 5); // 4 and 5 are ARGUMENTS
The argument 4 is assigned to the parameter a, and 5 to b. Parameters are names; arguments are values.
Arguments are assigned to parameters in the order they appear. Swap them and you get a different answer:
function subtract(a, b) {
return a - b;
}
console.log(subtract(10, 5));
console.log(subtract(5, 10));
An argument does not have to be a literal value. It can be a variable, or any expression. JavaScript works out its value first, then passes that value in:
let x = 5;
let y = 6;
console.log(multiply(x, y));
console.log(multiply(x + 1, 2));
The variable names do not need to match the parameter names. Inside multiply, the values are simply called a and b.
JavaScript is very relaxed about arguments:
So incorrect arguments give incorrect answers, silently:
function toCelsius(fahrenheit) {
return (5 / 9) * (fahrenheit - 32);
}
console.log(toCelsius("John")); // wrong type
console.log(multiply(4)); // too few: b is undefined
console.log(multiply(1, 2, 3, 4)); // too many: extras ignored
Missing arguments become undefined. Sometimes that is fine, but usually it is better to give the parameter a fallback value. Before 2015, that meant checking for undefined by hand. You will still see this in older code:
function myFunction(x, y) {
if (y === undefined) {
y = 2;
}
return x * y;
}
console.log(myFunction(5));
Since ES2015, a default parameter does the same job in one step: function myFunction(x, y = 2). Use that in your own code.
Inside a regular function, a built-in variable named arguments holds every value that was passed, whether or not it has a parameter name. With it, a function can accept any number of values. Here is a function that finds the highest of however many numbers you give it:
function findMax() {
let max = -Infinity;
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
console.log(findMax(1, 123, 500, 115, 44, 88));
Starting max at -Infinity guarantees the first real number is bigger. (Call findMax() with no arguments at all and that is exactly what you get back: -Infinity.)
arguments has a length and numbered positions, so it looks like an array, but it is not one: array methods like map do not work on it. Arrow functions (Function Arrow lesson) do not even have their own arguments.
A rest parameter (...name) does the same job as arguments, but gives you a real array, with a name you chose:
function sum(...args) {
let total = 0;
for (const arg of args) total += arg;
return total;
}
console.log(sum(4, 9, 16, 25, 29, 100, 66, 77));
arguments in older code, but write rest parameters in yours. They are clearer (the name says what the values are), they are real arrays, and they work in every kind of function.
If your values are already in an array, three dots in the call spread them out as separate arguments:
const scores = [72, 95, 88];
console.log(Math.max(scores)); // one argument: the whole array
console.log(Math.max(...scores)); // three arguments: 72, 95, 88
... gathers arguments into an array (rest). In a function call, ... spreads an array out into separate arguments (spread).
Simple values (numbers, strings, booleans) are passed by value: the function gets a copy, so changing it inside has no effect outside. Arrays and objects behave as if they are passed by reference: the function gets access to the very same array, so changes inside are visible outside:
function tryToChange(num, list) {
num = 100; // changes the local copy only
list.push(100); // changes the original array
}
let n = 1;
let arr = [1];
tryToChange(n, arr);
console.log(n);
console.log(arr);
(push adds an item to the end of an array; the Arrays section covers it.) Technically, JavaScript passes everything by value, but the "value" of an array or object is a reference to it, which is why it acts shared.
createButton("Save", true, false, 3) gets hard to read: what do true, false, and 3 mean? The fix is to pass one object with named settings. You will see how in the Objects section, which comes next.
undefinedWhat happens when a function is called with fewer arguments than parameters?
undefined0undefined, unless it has a default value.What does this log?
function count() {
return arguments.length;
}
console.log(count("a", 2, true, null));
4. arguments holds every value passed, even with no parameters listed, and even when a value is null.With const temps = [61, 75, 58];, which gives the lowest temperature?
Math.min(temps)Math.min(...temps)Math.min(temps[0])Math.min(...[temps])NaN. C only checks the first temperature.multiply with one, two, and four arguments. Note each resultfindMax using a rest parameter instead of argumentsMath.min and spread