Skip to main content

Rest Parameter | JavaScript

Updated 1 min readMarkdown
Rest Parameter | JavaScript

Rest Parameter

Rest parameter a modern feature of JavaScript that allows a function to store multiple arguments in a single array. It is an improved way of handling function parameter. Rest parameter is added in ES2015 or ES6 which improved the ability to handle parameter.

“

function name[...param]                 //... is the rest parameter
{
    // statement;
}

”

Let’s see how it works:

Image for post Image for post

Function without rest parameter; no error will be thrown even when arguments passed are more than the parameters, but only the first two arguments will be evaluated.

Image for post

But, while using rest parameter, it’s a different case. With the use of the rest parameter, we can gather any number of arguments into an array and perform any action with them.

Also, the rest parameter must be the last parameter in the list because a Comma is not permitted after the rest element.

Image for post

I hope this helps someone

Stay tuned

No comments yet. Be the first to comment.

More from this blog