
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is the difference between default and rest parameters in JavaScript functions?
Default Parameters
The default parameter came to handle function parameters with ease. You can easily set Default parameters to allow initializing formal parameters with default values. This is possible only if no value or undefined is passed.
Example
<html> <body> <script> // default is set to 1 function inc(val1, inc = 1) { return val1 + inc; } document.write(inc(10,10)); document.write("<br>"); document.write(inc(10)); </script> </body> </html>
Output
Rest Parameters
ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter. With this, set indefinite number of arguments as an array, which is Array instances.
Example
Let’s see the following code snippet −
<html> <body> <script> function addition(…numbers) { var res = 0; numbers.forEach(function (number) { res += number; }); return res; } document.write(addition(3)); document.write(addition(5,6,7,8,9)); </script> </body> </html>
- Related Articles
- What are Rest parameters in JavaScript functions?
- What is the difference between rest parameters and the arguments object in Javascript?
- Effective Function Signatures with Default and Rest Parameters in JavaScript
- How to arguments object with Rest, default, and destructured parameters in JavaScript?
- What is “Parameters without defaults after default parameters in JavaScript”
- What are Rest parameters in JavaScript?
- What are Default parameters in JavaScript?
- What are default-parameters for function parameters in JavaScript?
- What is the difference between functions and methods in JavaScript?
- What are default function parameters in JavaScript?
- What is the difference between anonymous and inline functions in JavaScript?
- What is the difference between closure and nested functions in JavaScript?
- What is difference between unescape() and escape() functions in JavaScript?
- What is the difference between custom and built-in functions in JavaScript?
- What is the difference between arguments and parameters in Python?

Advertisements