Facebook Pixel

Function Parameters and Arguments in JavaScript

Parameters, arguments, defaults, rest, and spread. Here is the complete guide to passing values into functions.

Function Parameters and Arguments in JavaScript

Functions receive values through parameters. Modern JavaScript has several ways to handle them: defaults, rest, and spread.

Parameters vs Arguments

  • Parameters: the names listed in the function definition.
  • Arguments: the actual values passed when calling.
function greet(name, greeting) { // parameters console.log(`${greeting}, ${name}`); } greet("Kunal", "Hello"); // arguments

Extra and Missing Arguments

  • If you pass more arguments than parameters, extras are ignored (but available via arguments or rest).
  • If you pass fewer, missing parameters are undefined.
function foo(a, b) { console.log(a, b); } foo(1, 2, 3); // 1 2 (3 ignored) foo(1); // 1 undefined

Default Parameters

function greet(name = "friend", greeting = "Hi") { console.log(`${greeting}, ${name}`); } greet(); // "Hi, friend" greet("Asha"); // "Hi, Asha" greet(undefined, "Yo"); // "Yo, friend"

Defaults apply when the argument is undefined or omitted. Other falsy values (0, "", null) do not trigger the default.

Rest Parameters

Collect remaining arguments into a real array:

function sum(...nums) { return nums.reduce((a, b) => a + b, 0); } sum(1, 2, 3, 4); // 10

Rest must be the last parameter. It is a true array (has map, filter, etc.), unlike arguments.

Spread in Calls

Pass array elements as individual arguments:

const nums = [1, 2, 3]; Math.max(...nums); // 3

Destructuring Parameters

function render({ name, age = 0 }) { console.log(name, age); } render({ name: "Kunal" }); // "Kunal 0"

The arguments Object

Available in regular (non-arrow) functions. Array-like, has length and indices, but no array methods:

function show() { console.log(arguments.length, arguments[0]); } show("a", "b"); // 2 "a"

Arrow functions do not have their own arguments. Use rest parameters in arrows.

The Takeaway

Parameters are names, arguments are values. Defaults fill in undefined. Rest collects extras into a real array. Spread expands an array into individual arguments. arguments is array-like and only in regular functions; prefer rest in modern code.

Parameters are the variable names listed in the function definition. Arguments are the actual values passed to the function when it is called.

If the caller passes undefined or omits the argument, the default value is used. Other falsy values like 0, null, or empty string do not trigger the default.

Rest parameters (...args) collect remaining arguments into a true array with methods like map and filter. arguments is array-like (has length and indices) but lacks array methods, and is not available in arrow functions.

Yes. Spread expands an array into individual arguments. For example, Math.max(...[1, 2, 3]) is the same as Math.max(1, 2, 3).

The extra arguments are ignored by the named parameters, but they are still accessible through the arguments object or a rest parameter. Missing arguments become undefined.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.