• 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

  • Web 3.0 may have died before it even started
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • Smoke Detector Randomly Goes Off Early Hours of The Morning
  • A List of WordPress Gutenberg Core Blocks
  • Which Neural DSP Archetype Plugins Should You Buy?
  • How to Copy Files Using the Copy Webpack Plugin (without copying the entire folder structure)
  • How To Install Eufy Security Cameras Without Drilling or Using Screws
  • How To Calculate A Javascript Date X Months Ago With Vanilla Javascript

Recent Comments

  • Casey Milne on A List of WordPress Gutenberg Core Blocks
  • Jay on Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • john on Deno Raises $21M – but is anyone using it yet?
  • Oranges on How To Store Users In Firestore Using Firebase Authentication
  • Precious on Fixing Sequel Pro SQL Encoding Error For Imported SQL Files

Copyright © 2022 · Dwayne Charrington · Log in

wpDiscuz