Logo
5 Oct 2016 | 1 min. (105 words)

Default parameters with ES6

With ES5 if you wish to set default parameters on a javascript function you would be required to check the value and supply a default if one hadn’t been set.

var myFunction = function(arg1, arg2){
    arg1 = arg1 || 25;
    arg2 = arg2 || true;

    //do something
}

Even this has problems as passing in 0 for arg1 or false for arg2 would both evaluate as “falsey” and therefore set the default values.

However with ES6 we can now set default parameters within the parameter definition resulting in cleaner and more readable code!

var myFunction = function(arg1 = 25, arg2 = true){   
    //do something
}
es6 javascript function parameters
Twitter Facebook

Moving from Wordpress to Hugo

Read why I've moved from Wordpress CMS to Hugo static site generator…

8 Tips to increase front end website performance

…

Related Links

  • LinkedIn
  • Twitter: @curtcode
  • Stack Overflow
  • GitHub: @curtiscde

Stack Exchange

profile for Curtis on Stack Exchange, a network of free, community-driven Q&A sites
Follow @curtiscde

Recent Posts

  • Displaying latest posts on your GitHub profile
  • Using Codecov within a monorepo
  • Displaying Strava stats using webhooks & GitHub Actions
  • Filtering duplicate data points on Chart.js
  • Next.js Typerite Template Boilerplate
Theme Bleak by zutrinken Published with Hugo
Menu