undefined being treated as a missing optional argument

Luke Hoban lukeh at microsoft.com
Wed Apr 11 19:01:20 PDT 2012

>> Just to start, it means a method like:
>>   Array.prototype.fill = function(value=null) { this.forEach((v,i)=>this[i]=value)}
>> wouldn't do the obvious thing when called as:
>>   someArray.fill(undefined)

I think it is debatable what is obvious in this case.  The wiki says that this should fill the array with 'undefined' (I believe the spec draft aims to say the same, but I can't tell where this is established).  But I think many JavaScript developers would expect this to fill the array with 'null'.

Some arguments in favor of for treating undefined the same as not-present:

1) Existing uses of default value patterns in JavaSscript use undefined as the sentinel for not present, so a semantics for default values in ES6 that behaves differently will not be usable as a shorthand for any of these existing APIs without changing semantics.

2) The fact that JavaScript (at least for user objects) currently doesn't differentiate between missing arguments and undefined arguments is a nice simplifying rule in the language that is easy to understand..

3) The example above, of wanting to have an API that allows explicitly passing in undefined to override a default value, seems outside of the common case (out of curiosity - are there any realistic example of this?).  If truly desired, it is easy to not use default values.  But the common case seems more likely to be to emulate what is done today - and avoid having any undefined values flow into the API call.


More information about the es-discuss mailing list