Parameters describe inputs; arguments supply them

A parameter is a name in a function definition. An argument is a value supplied when the function is called. In function greet(name) { return "Hello, " + name; }, name is the parameter. In greet("Sam"), "Sam" is the argument. You can run the examples in this article in a modern browser’s JavaScript console; they do not need a page, library, or build tool.

JavaScript does not require a call to provide exactly one argument for each parameter. Calling greet() leaves name with the value undefined, so the function returns "Hello, undefined". Calling greet("Sam", "Lee") still returns "Hello, Sam" because this function does not use the second argument. This flexibility is useful, but it can also hide mistakes. Decide which inputs are required, and handle optional ones deliberately.

Default parameters handle omitted and undefined arguments

A default parameter supplies a value when the corresponding argument is missing or explicitly undefined. For example, function greet(name = "friend") { return "Hello, " + name; } makes greet() and greet(undefined) both return "Hello, friend". Passing a value uses that value instead: greet("Sam") returns "Hello, Sam".

Defaults do not replace every value that seems empty. With that same function, greet(null) returns "Hello, null", and greet("") returns "Hello, ". JavaScript applies the default for undefined, not for null, an empty string, zero, or false. If those values should also trigger a fallback, check for them explicitly so the behavior is clear.

A default expression runs when it is needed, rather than once when the function is defined. Earlier parameters can also inform later defaults: function label(item, prefix = "Item") { return prefix + ": " + item; } accepts label("Book") or label("Book", "Gift"). Choose defaults that make sense for callers, and avoid silently substituting a value when a genuinely required argument is missing.

Rest parameters collect additional arguments

A rest parameter gathers remaining arguments into an array. For example, function total(...numbers) { return numbers.reduce((sum, number) => sum + number, 0); } makes total(2, 3, 4) return 9. With no arguments, numbers is an empty array and total() returns 0. Because it is an array, you can use array methods such as reduce, map, or filter on it.

A function can have named parameters before a rest parameter: function announce(title, ...names) { return title + ": " + names.join(", "); } makes announce("Guests", "Sam", "Lee") return "Guests: Sam, Lee". The rest parameter must be last, and a function can have only one. It collects arguments that have not already been assigned to earlier parameters.

Rest parameters collect values; they do not validate them. The total function assumes every argument is a number. If total("2", 3) is a possible call in your program, decide whether to reject the string or convert it before adding. This is especially important when arguments come from form fields, where entered values are commonly strings.

Return values make the result available to callers

A return statement ends the current function call and gives its value to the caller. In const answer = total(2, 3);, answer becomes 5 because total returns its calculation. A function without an executed return statement returns undefined. For example, function showTotal(a, b) { console.log(a + b); } prints a value, but const answer = showTotal(2, 3); assigns undefined to answer. Printing a result and returning it serve different purposes.

An early return can make missing-input behavior explicit. For example, function describe(name) { if (name === undefined) return "Name required"; return "Name: " + name; } distinguishes an omitted argument from a supplied one. That response is only one possible design: another function might use a default, while a function that cannot proceed might report an error. The important part is choosing behavior callers can understand.

When designing a function, ask three questions: Which arguments must callers provide? What should happen when an optional argument is omitted? What value should callers receive? Defaults answer the second question, rest parameters help with a variable number of inputs, and return statements answer the third. Keeping those choices visible makes functions easier to test and reuse.

Back to category ↗