Home Blog CV Projects Patterns Notes Book Colophon Search

ES6 using functions as default parameters

12 Jan, 2017

function returnCalled() {
  return "Called";
}

function doSomething(fn = returnCalled, two = "Two"){
  console.log(`fn(): ${fn()}, two: ${two}`);
}

doSomething()

Gives:

fn(): Called, two: Two

So using a function as a default parameter works fine in Node 6.

The same thing works if you explicitly set undefined as an argument:

doSomething(undefined, undefined);

Gives:

fn(): Called, two: Two

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.