Function Parameters
Parameters let a function take inputs, so one function can do the same job on different values. The parameters are the names listed in the parentheses of the declaration, and the arguments are the values passed in when the function is called.
function name(parameter1, parameter2) { // parameter1 and parameter2 hold the arguments}
name(argument1, argument2);Passing values in
Section titled “Passing values in”Without parameters, a function that adds an item to a basket needs one copy per item, as Introduction to Functions showed. With parameters, one function covers every item:
let basketTotal = 0;
function addItem(name, price) { basketTotal += price; console.log(`${name} added. Total: £${basketTotal.toFixed(2)}`);}
addItem('Bread', 1.45);addItem('Milk', 0.95);This prints:
Bread added. Total: £1.45Milk added. Total: £2.40Arguments are matched to parameters by position, so the first argument goes to name and the second to price. Inside the function, each parameter behaves like a local variable.
Missing and extra arguments
Section titled “Missing and extra arguments”JavaScript does not check how many arguments a call passes. A parameter with no matching argument is undefined, and an argument with no matching parameter is ignored.
function describe(name, colour) { console.log(name, colour);}
describe('Tulip');describe('Rose', 'red', 'extra');This prints:
Tulip undefinedRose redDefault values
Section titled “Default values”A default value, written with = after the parameter’s name, is used when the argument is missing or undefined. Any other value, null included, replaces the default.
function addTask(title, priority = 'normal') { console.log(`${title} (${priority})`);}
addTask('Pay the gas bill', 'high');addTask('Water the plants');addTask('Book a dentist', undefined);addTask('Call the bank', null);This prints:
Pay the gas bill (high)Water the plants (normal)Book a dentist (normal)Call the bank (null)Older code set defaults inside the body with priority = priority || 'normal', which also replaced valid values such as 0 and the empty string. Default parameters avoid that problem.
Any number of arguments
Section titled “Any number of arguments”A rest parameter, written with ... before the last parameter’s name, collects the remaining arguments into an array:
function logAverage(label, ...temperatures) { let sum = 0; for (const t of temperatures) { sum += t; } console.log(`${label}: ${sum / temperatures.length}°C`);}
logAverage('Monday', 12, 15, 9);logAverage('Tuesday', 20, 22);This prints:
Monday: 12°CTuesday: 21°CA rest parameter is a real array, so array methods work on it. It replaces the older arguments object, which is array-like but not an array, and which does not exist in arrow functions.
Passing a function as an argument
Section titled “Passing a function as an argument”A function is a value, so it can be passed to another function as an argument. The receiving function then decides when to call it. Here printTasks loops over a list, and the function passed as printOne decides how each task looks:
function printPlain(task) { console.log(task);}
function printChecklist(task) { console.log(`[ ] ${task}`);}
function printTasks(tasks, printOne) { for (const task of tasks) { printOne(task); }}
const tasks = ['Buy stamps', 'Return library books'];printTasks(tasks, printPlain);printTasks(tasks, printChecklist);This prints:
Buy stampsReturn library books[ ] Buy stamps[ ] Return library booksThe functions are passed by name without parentheses, because printPlain is the function itself, while printPlain() would call it and pass its result instead. A function passed in this way is called a callback, which Callback Functions covers.
Objects as arguments
Section titled “Objects as arguments”An argument that is an object passes a reference to the same object, so a function that changes one of its properties changes the caller’s object. Assigning a new object to the parameter only changes the local name, and leaves the caller’s object alone.
function rename(book) { book.title = 'Changed';}
function replace(book) { book = { title: 'Replaced' };}
const novel = { title: 'Original' };rename(novel);console.log(novel.title);replace(novel);console.log(novel.title);This prints:
ChangedChangedPass By Reference explains how objects and primitive values are passed.
Steve Griffith covers this topic in his video JavaScript Function Parameters.