JavaScript Rest parameter Last Updated : 13 Aug, 2024 Comments Improve Suggest changes Like Article Like Report The JavaScript Rest parameter allows a function to accept an indefinite number of arguments as an array. It is represented by three dots (...) followed by the parameter name and must be the last parameter in the function, enabling flexible and dynamic argument handling.Syntax//... is the rest parameter (triple dots) function functionname(...parameters) { statement; }Note: When ... is at the end of the function parameter, it is the rest parameter. It stores n number of parameters as an array. Let's see how the rest parameter works: Let's see how the rest parameter works.Example 1: Without Using the Rest ParameterJavascript code demonstrating the passing arguments more than the parameters without using rest parameter JavaScript function fun(a, b){ return a + b; } console.log(fun(1, 2)); // 3 console.log(fun(1, 2, 3, 4, 5)); // 3 Output3 3 Explanation: In the above code exampleExtra arguments beyond parameters are ignored without error.Use ... to collect additional arguments into an array.Rest parameters allow flexible argument handling for functions.Example 2: Using the Rest ParameterJavaScript code demonstrating the addition of numbers using the rest parameter. JavaScript // es6 rest parameter function fun(...input) { let sum = 0; for (let i of input) { sum += i; } return sum; } console.log(fun(1, 2)); //3 console.log(fun(1, 2, 3)); //6 console.log(fun(1, 2, 3, 4, 5)); //15 Output3 6 15 We were able to get the sum of all the elements that we enter in the argument when we call the fun() function. We get the sum of all the elements in the array by making use of the for..of loop which is used to traverse the iterable elements inside an array. Note: The rest parameter has to be the last argument, as its job is to collect all the remaining arguments into an array. So having a function definition like the code below doesn't make any sense and will throw an error. Example 3: Using Rest Parameter with Other ParametersIn this example, we are using the rest parameter with some other arguments inside a function. javascript // rest with function and other arguments function fun(a, b, ...c) { console.log(`${a} ${b}`); //Mukul Latiyan console.log(c); //[ 'Lionel', 'Messi', 'Barcelona' ] console.log(c[0]); //Lionel console.log(c.length); //3 console.log(c.indexOf('Lionel')); //0 } fun('Mukul', 'Latiyan', 'Lionel', 'Messi', 'Barcelona'); OutputMukul Latiyan [ 'Lionel', 'Messi', 'Barcelona' ] Lionel 3 0 Explanation:The rest parameter collects extra arguments into an array, handling more than the defined parameters.In the function, c[0] returns 'Lionel' from the rest parameter array.The rest parameter is an array, allowing use of standard array methods for further manipulation.We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article. Comment More infoAdvertise with us Next Article JavaScript Rest parameter Y YugShah Follow Improve Article Tags : JavaScript Web Technologies javascript-operators Similar Reads How to retrieve GET parameters from JavaScript ? In order to know the parameters, those are passed by the âGETâ method, like sometimes we pass the Email-id, Password, and other details. For that purpose, We are using the following snippet of code. When you visit any website, ever thought about what the question mark '?' is doing in the address bar 2 min read JavaScript Know the value of GET parameters from URL In this article, we will see how to get the GET parameters from a URL using JavaScript. In order to know the parameters that are passed by the "GET" method, sometimes we need to pass the Email and other details. For that purpose, we are using the following snippet of code. Example 1: This example ge 1 min read JavaScript JSONP What is JSONP?The **XMLHttpRequest (XHR)** is a tool used to retrieve data from a server. Once the data is received by the browser, it can be converted from a JSON string into a JavaScript object using the `JSON.parse()` method. However, XHR encounters an issue known as the **same-origin policy**. T 3 min read Explain the Rest parameter in ES6 In this article, we will try to understand the details associated with the rest parameter in ES6 which includes the syntax of rest parameters, and how to use this with the help of certain examples. The rest parameter is an improved way to handle function parameters, allowing us to handle various inp 3 min read TypeScript Rest Parameters and Arguments TypeScript Rest Parameters allow functions to accept an indefinite number of arguments of the same type, collecting them into an array. Arguments refer to the actual values passed to a function when it's invoked, while Rest Parameters provide a way to handle multiple arguments as an array within the 2 min read What are Request Parameters in Postman ? Postman, a powerful tool for API testing, offers many features that make it easier to test, share, and document APIs. One of the crucial aspects of API testing using Postman involves handling request parameters. In this article, we will see what request parameters are, how to send them using Postman 4 min read How Are Parameters Sent In An HTTP POST Request? HTTP POST requests are widely used in web development to send data from a client to a server. Whether you're submitting a form, uploading a file, or sending JSON data via an API, understanding how parameters are sent in an HTTP POST request is important. In this article, weâll explore how are parame 4 min read How to Access Request Parameters in Postman? Request parameters are additional pieces of information sent along with a URL to a server. They provide specific details about the request, influencing the server's response. Parameters typically follow a 'Key=Value' format and are added to the URL in different ways depending on their type.Table of 4 min read Reading Path Parameters in Node.js Path parameters are an essential part of RESTful APIs, allowing the server to capture values from the URL and use them in request handling. This capability is crucial for creating dynamic and flexible endpoints. In Node.js, especially when using frameworks like Express.js, handling path parameters i 2 min read Lodash _.rest() Method Lodash _.rest() method is used to create a function that calls the given function with the binding of the created function along with an array of arguments from the start position and beyond.Syntax:_.rest( func, start );Parameters: func: It is the function that is used to apply a rest parameter.star 2 min read Like