Rest parameters in vanilla JavaScript

In JavaScript, a rest parameter is a function parameter that gets assigned an array with all of the arguments that are passed from that point on in a function.
You define a rest parameter by creating a parameter prefixed with …. Any arguments provided at or beyond the rest parameter on a function get combined into an array that’s assigned to the rest parameter’s name.
You can only have one rest parameter on a function.


This content originally appeared on Go Make Things and was authored by Go Make Things

In JavaScript, a rest parameter is a function parameter that gets assigned an array with all of the arguments that are passed from that point on in a function.

You define a rest parameter by creating a parameter prefixed with .... Any arguments provided at or beyond the rest parameter on a function get combined into an array that’s assigned to the rest parameter’s name.

You can only have one rest parameter on a function. In the example below, ...moreArgs is a rest parameter.

function logStuff (arg1, arg2, ...moreArgs) {

	// Logs arg1
	console.log(arg1);

	// Logs arg2
	console.log(arg2);

	// Logs an array of any other arguments you pass in after arg2
	console.log(moreArgs);

}

// In this example...
// arg1 = 'chicken'
// arg2 = 'tuna'
// moreArgs = ['chips', 'cookie', 'soda', 'delicious']
logStuff('chicken', 'tuna', 'chips', 'cookie', 'soda', 'delicious');

In yesterday’s article on default function parameters, we looked at an add() function for adding numbers together.

/**
 * Add two numbers together
 * @param  {Number} num1 The first number
 * @param  {Number} num2 The second number
 * @return {Number}      The sum of both numbers
 */
function add (num1, num2) {
	return num1 + num2;
}

If you wanted to add two or more numbers together, you could use a rest parameter to capture all numbers passed in, regardless of how many there were.

Here’s our add() function rewritten with a rest parameter.

function add (...nums) {

	// Set a starting total
	let total = 0;

	// Add each number to the total
	for (let num of nums) {
		total += num;
	}

	// Return to the total
	return total;

}

⏰🦉 Early Bird Sale! Today through Monday, get 40% off registration in the next session of the Vanilla JS Academy.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-03-15T14:30:00+00:00) Rest parameters in vanilla JavaScript. Retrieved from https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/

MLA
" » Rest parameters in vanilla JavaScript." Go Make Things | Sciencx - Wednesday March 15, 2023, https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/
HARVARD
Go Make Things | Sciencx Wednesday March 15, 2023 » Rest parameters in vanilla JavaScript., viewed ,<https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/>
VANCOUVER
Go Make Things | Sciencx - » Rest parameters in vanilla JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/
CHICAGO
" » Rest parameters in vanilla JavaScript." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/
IEEE
" » Rest parameters in vanilla JavaScript." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/. [Accessed: ]
rf:citation
» Rest parameters in vanilla JavaScript | Go Make Things | Sciencx | https://www.scien.cx/2023/03/15/rest-parameters-in-vanilla-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.