YogeshChauhan . com

Optional arguments, Default parameters and REST parameters in JavaScript

in JavaScript on January 12, 2021

Optional Arguments

Take a look at this JavaScript functions post first before reading further: Different Types of Functions in JavaScript

Now that we have a better understanding of JavaScript Functions, we can now provide optional arguments to those functions.

JavaScript doesn’t raise an error if the function has multiple arguments, but we don’t specify them when we call the function. For example,


function printTime(times, message) {
  for(var a = 0; a < times; a++) {
    console.log(times);
  }
}
printTime(3);

//output
3
3
3 
 

JavaScript would raise an error and return undefined if we were to use message in the function but we are not using it.

Just like this example.


function multiply(a, b) {
  return a * b
}
multiply(5, 2)  // output 10
multiply(5)     // output NaN !

There are few ways to handle optional arguments if we are using those arguments in the function. One of those ways is default parameters.

Default parameters

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

Let’s modify the example above by adding default parameters.


function multiply(a, b = 1) {
  return a * b 
}
                
multiply(5, 2)          // output 10
multiply(5)             // output 5
multiply(5, undefined)  // output 5

REST parameters

The rest parameter syntax allows to represent an indefinite number of arguments as an array.

A function’s last parameter can be prefixed with … (3 dots) which will cause all remaining arguments to be placed within a JavaScript array.

Syntax and example:


function myFunc(...args) {
  console.log(args);
}

//example

myFunc( 1, 2, 3, 4, 5);       // output [1,2,3,4,5]

One more example:


function myFun(a, b, ...manyMoreArgs) {
  console.log("a", a)
  console.log("b", b)
  console.log("manyMoreArgs", manyMoreArgs)
}
                
myFun("one", "two", "three", "four", "five", "six")

// a, one
// b, two
// manyMoreArgs, [three, four, five, six]

In the example above, the first argument is mapped to “a” and the second to “b” and the third argument will be an array that contains the 3rd, 4th, … till nth — all the arguments we add.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How Do You Make a Private VPN?MiscWordPress: How to display slider repeater fields in ACF?WordPressHow to import MySQL small sample database into phpMySQL?SQL/MySQLHow to show widgets on the Appearance tab in WordPress?WordPressHow to create Glowing Text using CSS?CSSWhat Is a Graph Database?Misc