• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Default Parameter Values On Javascript Functions

ES2015 (aka ES6) · February 3, 2015

It’s time for another ES6 post. This time, we’re talking about arguably one of the best additions to Javascript that is up there with modules and classes. I am talking about the ability to specify default parameter values on a Javascript function.

This is one of the biggest missing pieces in Javascript in my opinion. I have encountered the need for this numerous times and while you can definitely implement a solution that does the job, there has been no support for native parameter values until ES6.

Previously we had to implement default values using the typeof operator like this for ES5 capable browsers:

[code]
function myFunc(param1, param2, param3) {
// param2 and param3 are optional parameters
// If we don’t have a param value, we set a default value which is currently an empty string ”
param2 = (typeof param2 !== ‘undefined’) ? param2 : ”;
param3 = (typeof param3 !== ‘undefined’) ? param3 : ”;
}

myFunc(‘someValue’);
[/code]

While this approach definitely works and has served us well for a long time, we finally have native support for default parameter values on functions in ES6.

If you have used any other language like Java or PHP, then the default parameter values in Javascript’s ES6 specification will look familiar to you.

Default parameter values the ES6 way:

[code]
function myFunc(param1, param2=”, param3=”) {
// param2 and param3 are optional parameters
console.log(param1, param2, param3);
}

myFunc(‘someValue’);
[/code]

Browser support for default parameter values

Unfortunately browser support is fairly nonexistent for default values. Even with ES6 features turned on in Chrome, they’re not supported. Firefox seems to fare pretty well for ES6 support and if you paste the above code into the Firefox console, it will run. In-fact, Firefox is currently the only browser that supports default parameter values.

You can keep track of browser support on the Mozilla documentation page for default parameter values here. In the mean time, you can still use them, just remember to use a transpiler like 6to5 or Traceur if you do.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • I Joined Truth Social Using a VPN and Editing Some HTML to Bypass the Phone Verification
  • How To Install Eufy Security Cameras Without Drilling or Using Screws
  • How To Get The Hash of A File In Node.js
  • Wild Natural Deodorant Review
  • The Most Common iPhone Passcodes (and how to guess them)
  • How to Record With the Neural DSP Quad Cortex in Reaper (DI and USB Recording)
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How To Paginate An Array In Javascript

Recent Comments

  • CJ on Microsoft Modern Wireless Headset Review
  • Dwayne on Microsoft Modern Wireless Headset Review
  • CJ on Microsoft Modern Wireless Headset Review
  • john on Microsoft Modern Wireless Headset Review
  • Dwayne on Why You Should Be Using globalThis Instead of Window In Your Javascript Code

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz