JavaScript Rest parameter Last Updated : 13 Aug, 2024 Summarize Comments Improve Suggest changes Share 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 JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis 8 min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read Like