Facebook Pixel

JavaScript Arguments Object and Parameters Explained

Functions receive values as arguments. Here is the difference between parameters and arguments and how the arguments object works.

JavaScript Arguments Object and Parameters Explained

The terms "parameter" and "argument" are often confused. Knowing the difference matters in interviews and in reading docs.

Parameters vs Arguments

  • Parameters are the variables listed in the function definition.
  • Arguments are the actual values passed when the function is called.
function greet(name, greeting) { // name, greeting are parameters console.log(greeting, name); } greet("Kunal", "Hello"); // "Kunal", "Hello" are arguments

Default Parameters

You can give parameters default values, used when the caller passes undefined (or omits the argument):

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

Rest Parameters

Use ... to collect remaining arguments into an array:

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

The arguments Object

Every regular (non-arrow) function has an arguments object. It is array-like (indexed, has length) but not a true array.

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

It does not have array methods like map or filter. Convert with Array.from(arguments) or spread: [...arguments].

Arrow Functions Do Not Have arguments

Arrow functions do not define their own arguments. They reference the arguments of the closest enclosing regular function. Use rest parameters in arrows instead.

The Takeaway

Parameters are variables in the definition; arguments are the values you pass. Default and rest parameters are modern alternatives to fiddling with arguments. The arguments object exists only in regular functions, not arrows, and is array-like but not a true array.

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

An array-like object available inside regular (non-arrow) functions. It holds the actual arguments passed, indexed by position, with a length property, but no array methods.

No. Arrow functions do not have their own arguments. If you reference arguments inside an arrow, it refers to the arguments of the enclosing regular function. Use rest parameters (...args) in arrows instead.

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.

Use Array.from(arguments) or the spread operator [...arguments]. Then you can call map, filter, reduce, and other array methods on it.

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.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.