Default Parameter in JavaScript - IndianTechnoEra
Latest update Android YouTube

Default Parameter in JavaScript

 

What is default parameter in JavaScript

Default parameter return itself when the function don't have passing any argument which is need to perform the task by function.


It return the value when not any parameter is passing through the function as an argument and help to show the error.


Example 1: Predefined default value printing and also to pass the argument
function activity (name = 'Default Name', act = 'Default Act') {
  return `${name} loves ${act}`
}

console.log(activity('sam', 'blogging')) // sam loves blogging
console.log(activity()) // Default Name loves Default Act


Example 2: Fixed Predefined default value printing and also to pass the argument which will arise error
var dPerson = {
  name: { fn: 'Sam', ln: 'blogging' },
  act: 'See'
}

function lAct (p = dPerson) {
  return `${p.name.fn} loves ${p.name.ln}`
}
console.log(lAct()) // Default Name loves Default Act
console.log(lAct('sam', 'blogging')) // TypeError

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.