0

Default parameters in javscript function declerations can be achieved with a simple assignment as follows:

function foo(arg1 = 'default1', arg2 = 'default2') { ... }

But how can I have default parameters for function expressions, as the following does NOT work in chrome v47:

var foo = function (arg1 = 'default1', arg2 = 'default2') { ... }

Any pointers would be helpful.

Grateful
  • 9,685
  • 10
  • 45
  • 77

1 Answers1

-1

You can't do this in javascript:

function foo(arg1 = 'default1', arg2 = 'default2') { ; }

See in your console, it gives syntax error. You can do this:

function foo(arg1, arg2) {
   if ("undefined" == typeof arg2) {
       arg2 = "default2";
   }
}

var foo2 = function(arg1, arg2) {
   if ("undefined" == typeof arg2) {
       arg2 = "default2";
   }
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105