Short answer
The safest bet is undefined and should work almost everywhere. Ultimately, however, you cannot fool the function being called, assuming that you really omitted the parameter.
If you rely on using null just because it is shorter, consider declaring a variable named _ as beautiful shorthand for undefined :
(function() {
More details
First, know that:
typeof undefined evaluates to "undefined"typeof null evaluates to "object"
So, suppose the function takes an argument, which is expected to be of type "number" . If you provide null as a value, you give it "object" . Semantics is disabled. one
As developers continue to write more and more reliable javascript code, there is an increasing chance that the functions you call explicitly check the parameter value for undefined , unlike the classic if (aParam) {...} . If you continue to use null interchangeably with undefined just because both of them force to false .
Remember, however, that in fact the function can tell if the parameter was really omitted (compared to undefined ):
f(undefined);
Footnote
- As long as
undefined also does not belong to the type of "number" , this whole job should be a type that is not a type. That is why the value accepted by uninitialized variables and the default value returned for functions.
Doug Paul Aug 02 2018-12-12T00: 00Z
source share