The arguments object in JavaScript functions

Within a function, you can use the arguments variable to get an array-like list of all of the arguments passed into the function.
You don’t need to define it ahead of time. It’s a native JavaScript object.
function add (num1, num2) { // logs [num1, num2] (with their values) console.log(arguments); // logs the value of `num1` console.log(arguments[0]); // logs the value of `num2` console.log(arguments[1]); // … } This is particularly useful if you would rather allow an unlimited number of arguments to be passed in to your function.

Within a function, you can use the arguments variable to get an array-like list of all of the arguments passed into the function.

You don’t need to define it ahead of time. It’s a native JavaScript object.

function add (num1, num2) {

	// logs [num1, num2] (with their values)
	console.log(arguments);

	// logs the value of `num1`
	console.log(arguments[0]);

	// logs the value of `num2`
	console.log(arguments[1]);

	// ...

}

This is particularly useful if you would rather allow an unlimited number of arguments to be passed in to your function.

Let’s say you wanted to be able to pass an unlimited amount of numbers into add() and add them together. The arguments variable is perfect for this!

Remember, arguments is array-like but not actually an array. To use array methods like Array.forEach() with it, you need to convert it to an array with the Array.from() method.

function add () {

	// Set a starting total
	let total = 0;

	// Add each number to the total
	Array.from(arguments).forEach(function (num) {
		total += num;
	});

	// Return to the total
	return total;

}

In the example above, we’re defining a default variable total with a value 0. We use Array.from() to convert arguments to an array, and an Array.forEach() loop to iterate over each argument and add it to the total. Then we return the total.

If someone passes in no arguments, it returns 0. Otherwise, they can add one or more numbers together.

// returns 0
add();

// returns 4
add(4);

// returns 36
add(4, 2, 11, 19);

Print Share Comment Cite Upload Translate
APA
Go Make Things | Sciencx (2024-03-28T17:52:30+00:00) » The arguments object in JavaScript functions. Retrieved from https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/.
MLA
" » The arguments object in JavaScript functions." Go Make Things | Sciencx - Friday March 19, 2021, https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/
HARVARD
Go Make Things | Sciencx Friday March 19, 2021 » The arguments object in JavaScript functions., viewed 2024-03-28T17:52:30+00:00,<https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/>
VANCOUVER
Go Make Things | Sciencx - » The arguments object in JavaScript functions. [Internet]. [Accessed 2024-03-28T17:52:30+00:00]. Available from: https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/
CHICAGO
" » The arguments object in JavaScript functions." Go Make Things | Sciencx - Accessed 2024-03-28T17:52:30+00:00. https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/
IEEE
" » The arguments object in JavaScript functions." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/. [Accessed: 2024-03-28T17:52:30+00:00]
rf:citation
» The arguments object in JavaScript functions | Go Make Things | Sciencx | https://www.scien.cx/2021/03/19/the-arguments-object-in-javascript-functions/ | 2024-03-28T17:52:30+00:00
https://github.com/addpipe/simple-recorderjs-demo