default parameters in node.js

hownowbrowncow picture hownowbrowncow · Feb 17, 2016 · Viewed 33.2k times · Source

How does one go about setting default parameters in node.js?

For instance, let's say I have a function that would normally look like this:

function(anInt, aString, cb, aBool=true){
   if(bool){...;}else{...;}
   cb();
}

To call it would look something like this:

function(1, 'no', function(){
  ...
}, false);

or:

function(2, 'yes', function(){
  ...
});

However, it doesn't seem that node.js supports default parameters in this manner. What is the best way to acomplish above?

Answer

mikemaccana picture mikemaccana · Jan 6, 2017

2017 answer: node 6 and above include ES6 default parameters

var sayMessage = function(message='This is a default message.') {
  console.log(message);
}