Official JS docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters says ‘In JavaScript, function parameters default to [pseudo-value ]undefined [when the the parameters are omitted].’ (and indeed I've always found this to be the case), plus it seems to suggest (so as I routinely do in my for in calling my own JS functions) that caller can specify that {the default value be used for an optional parameter} by simply there ensuring for that optional parameter than ‘undefined is passed’ explicitly. However I'm 1st-finding doing the latter causes errors else other odd behavior for the optional parameters of JS built-in methods -at least ‘new Date’ and ‘Array.prototype.splice’.
For instance, JavaScript simple method call of form ‘new Date(yr,moi,d,hr,min,s,ms)’ where one of these parameters which can be optional so is then given ‘undefined’ (so to there requests the default value be used) then gives (seemingly wrong) ‘Invalid date’ {in at least in top web browsers Chrome & Firefox} even tho the official docs seem to suggest the result should be the valid date which is returned when the ‘undefined’ parameter(s) have have simply been omitted, as in case (1 of each of the 5 optional parameters) of:
- ‘new Date(2020,6,undefined)’ gives Invalid when seems should give ‘new Date(2020,6)’
- ‘new Date(2020,6,15,undefined)’ gives Invalid when seems should give ‘new Date(2020,6,15)’
- ‘new Date(2020,6,15,12,undefined)’ gives Invalid when seems should give ‘new Date(2020,6,12)’
- ‘new Date(2020,6,15,12,30,undefined)’ gives Invalid when seems should give ‘new Date(2020,6,12,30)’
- ‘new Date(2020,6,15,12,30,30,undefined)’ gives Invalid when seems should give ‘new Date(2020,6,15,12,30,30)’
But ‘new Date’ official docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Individual_date_and_time_component_values say for these parameters from ‘day’-down are optional per its 1st ‘[, day [, hours [, minutes [, seconds [, milliseconds]]]]]’, so seems ‘undefined’ would work here.
So, as a hack, would it work by instead calling with value ‘null?’ not ‘undefined’ (even tho that would seem wrong). For at least ‘new Date’ in Chrome, I tried the hack of using ‘null’ instead of ‘undefined’ for optional parameter(s), finding oddly that works but just sometimes & bizarrely: for instance ‘new Date(2020,6,15,null)’ is same as ‘new Date(2020,6,15)’, BUT ‘new Date(2020,6,null)’ instead of giving ‘new Date(2020,6)’ it bizarrely gives that minus 1 day. and ‘new Date(2020,null)’ should not be allowed if ‘null’ is reduced to 0 as it often is but instead gives valid ‘new Date(2020,1)’ --bazar!, but certainly not predictable enough to be a fix ‘undefined’ failing.
More important, to test {this: problems when {passing undefined for omitted}} on other built-ins, searching for other JS built-in methods with optional parameters via https://developer.mozilla.org/en-US/search?q=optional, I find https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice I see ‘[0,1,2].splice(1)’ expectedly returns ‘[1, 2]’ BUT ‘[0,1,2].splice(1,undefined)’ returns mysterious ‘[]’.
So what's wrong and how to best fix?
It seems hard to believe that this late in JS development that these core could have such simple bugs for their optional optional, so I gather I must somehow be doing something wrong. But then if so, how does one explicitly request a built-in function be set its optional parameter to use its default-value w/o the {ugliness: needless and possibly problematic redundancy} of {having to explicitly {call the function with, so know & compute} every defaulted parameter's default value, nor explicitly code every possible call of the function its every every possible parameter omission}?