

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define rest parameter for a function in JavaScript?
With rest parameter, you can represent a number of arguments as an array. ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter.
Example
Let’s see the following code snippet to define rest parameter.
<html> <body> <script> function addition(…numbers) { var res = 0; numbers.forEach(function (number) { res += number; }); return res; } document.write(addition(3)); document.write(addition(9,10,11,12,13)); </script> </body> </html>
- Related Questions & Answers
- How to set a default parameter value for a JavaScript function?
- How to define a function in JavaScript?
- How to define a JavaScript function using Function() Constructor?
- Usage of rest parameter and spread operator in JavaScript?
- How to define global variable in a JavaScript function?
- How to define a JavaScript function using the expression?
- How to pass an object as a parameter in JavaScript function?
- How do we use function literal to define a function in JavaScript?
- How to define functions inside a function body in JavaScript?
- How to define an Arrow Function in JavaScript?
- How to define custom sort function in JavaScript?
- How to test if a parameter is provided to a function in JavaScript?
- How to define a function in Python?
- How to define methods for an Object in JavaScript?
- How to pass a function as a parameter in Java
Advertisements