DEV Community

Cover image for ES6: Default parameters explained
Naftali Murgor
Naftali Murgor

Posted on • Edited on

5 1

ES6: Default parameters explained

Introduction

ES2015(ES6) introduced default parameters. Let's jump right in and learn about default parameters.

Default parameters

What would happen if we called a function with all or some parameters missing? It turns out JavaScript assigns undefined to the missing arguments.

Let's see example in code:

const add = (num1, num2) => num1 + num2

const sum = add(2) // one argument is missing // gets called as 2 + undefined
console.log(sum) // prints NaN
Enter fullscreen mode Exit fullscreen mode

Default parameters allow us to define a default parameter value and will be used when no argument is provided for the paramter during funcion call:

const main = (port = 3000) => {
  // possible code ommitted here
}
main() //port will default to value of 3000
main(5000) // call main with 5000
Enter fullscreen mode Exit fullscreen mode

Another dummy example:

// add default parameters at the end of parameter list
const restoreWallet = (privateKey, dumpToJson=true) => {
  // posible code omitted
}

const myWallet = restoreWallet('0xFEEDBEEFFEEDBEEF', false) // dumpToJson supplied as false
const myWalletTwo = restoreWallet('0x05417') // dumpToJson defaults to true if not supplied


const fetchItems = async (storeName, keys = []) => {
  // possible code omitted here

}
const itemStore = await fetchItems('Electronics') // keys defaults to an empty array object
Enter fullscreen mode Exit fullscreen mode

Summary

Default parameters allow us to provide a default value for the argument when not supplied during the function call.

Default parameters are added at the end of the paremeter list.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
Comment hidden by post author
Collapse
 
j471n profile image
Jatin Sharma

Exactly we pass default parameters in the end of all the other parameters.

Collapse
 
naftalimurgor profile image
Naftali Murgor

Thanks for the feedback. Will update to fix the error

Some comments have been hidden by the post's author - find out more

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay