Making sense of “senseless” JavaScript features:
– 0.1 + 0.2 and the floating point format
– Type coercion
– Automatic semicolon insertion
– Why so many bottom values?
– Increment (++) and decrement (--)
https://www.smashingmagazine.com/2023/12/making-sense-of-senseless-javascript-features/
1/ W.r.t. optional parameters, JavaScript having both `undefined` and `null` becomes useful because we can distinguish between a parameter being `null` and it being omitted. (Probably not enough of an upside to outweigh the downsides elsewhere.)
In light of this, I’m wondering if we should try to never explicitly use `undefined`—unless we need to skip a positional parameter:
func(1, undefined, 3)
Often better: named parameters (see link below).
https://exploringjs.com/impatient-js/ch_callables.html#parameter-handling
2/ Note that we can create our own special “parameter is missing” values via default parameter values (=we don’t really need a built-in `undefined`):
const MISSING = Symbol('MISSING');
function f(param = MISSING) {
return param === MISSING ? 'MISSING' : param;
}
Using this function:
> f()
'MISSING'
> f(undefined) // also triggers the default value
'MISSING'
> f(null)
null
> f('abc')
'abc'
@rauschma would it be better to use `(void) 0` instead?
@functionalscript I meant: If we have parameter default values, we don’t need a special “parameter is missing” value.