Default parameter values in Javascript function definitions

…are very poorly supported. See here.

Instead just use this inside the function:

var b = typeof b !== 'undefined' ?  b : 1;

…where 1 is the default value.

Update: Better to use

if (typeof optionalArg === 'undefined') { optionalArg = 'default'; }

…because there’s really no need for a ternary. Detail here.

Leave a Reply