Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing in NULL as a parameter in ES6 does not use the default parameter when one is provided

Is there a known reason why passing in null as a parameter in ES6 does not use the default parameter when one is provided?

function sayHello(name = "World") {     console.log("Hello, " + name + "!"); }  sayHello("Jim");     // Hello, Jim! sayHello(undefined); // Hello, World! sayHello(null);      // Hello, null! 
like image 982
alacy Avatar asked Sep 22 '15 19:09

alacy


People also ask

What is default parameter in es6?

Default parameters allow us to initialize functions with default values. A default is used when an argument is either omitted or undefined — meaning null is a valid value. A default parameter can be anything from a number to another function.

What is default parameter can we use default parameter for first parameter in function?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

Can I pass NULL as a parameter in JavaScript?

Conclusion. In JavaScript, the condition “arg == null” can be used to check passed arguments for null values.

Which kind of parameters Cannot have a default value?

An IN OUT parameter cannot have a default value. An IN OUT actual parameter or argument must be a variable.


1 Answers

This is not that obvious

I've read some comments of why undefined is completely different than null and that's why it explains the current behavior of default parameters.

One could argue that explicitly passing undefined should not trigger the default value substitution because when I have a function:

const f = (x = 'default') => console.log(x); 

I would like it to print "default" when I run it as:

f(); 

but I would like it to print "undefined" when I explicitly run it as:

f(undefined); 

because otherwise why would I use f(undefined) in the first place? Clearly my intention here is to provide some argument instead of leaving it out.

Examples to the contrary

Now, consider this function:

const g = (...a) => console.log(JSON.stringify(a)); 

When I use it as:

g(); 

I get: []

But when I use it as:

g(undefined); 

I get: [null]

which clearly demonstrates that:

  1. passing undefined is not the same as not passing an argument at all
  2. sometimes null can be a default value instead of undefined

TC39 decision

Some background on the current behavior of the default parameters can be seen in the July 24 2012 Meeting Notes by TC39:

  • https://github.com/rwaldron/tc39-notes/blob/master/meetings/2012-07/july-24.md

Incidentally, it shows that explicitly passing undefined originally did not trigger the default value in the first draft and there was a discussion about whether or not it should do that. So as you can see the current behavior was not so obvious to the TC39 members as it now seems to be to people who comment here.

Other languages

That having been said, the decision of what should and what should not trigger the default value substitution is completely arbitrary at the end of the day. Even having a separate undefined and null can be though of as quite strange if you think about it. Some language have only undefined (like undef in Perl), some have only null (like Java), some languages use equivalents of false or an empty list or array for that (like Scheme where you can have an empty list or #f (false) but there is no equivalent of null that would be distinct from both an empty list and a false value) and some languages don't even have equivalents of null, false or undefined (like C which uses integers instead of true and false and a NULL pointer which is actually a normal pointer pointing to address 0 - making that address inaccessible even when mapped by any code that tests for null pointers).

What you can do

Now, I can understand your need to substitute default values for null. Unfortunately this is not a default behavior but you can make a simple function to help you:

const N = f => (...a) => f(...a.map(v => (v === null ? undefined : v))); 

Now every time you want defaults substituted for null values you can use it like this. E.g. if you have this function from one of the examples above:

const f = (x = 'default') => console.log(x); 

it will print "default" for f() and f(undefined) but not for f(null). But when you use the N function defined above to define the f function like this:

const f = N((x = 'default') => console.log(x)); 

now f() and f(undefined) but also f(null) prints "default".

If you want somewhat different behavior, e.g. substituting default values for empty strings - useful for environment variables that can sometimes be set to empty strings instead of not existing, you can use:

const N = f => (...a) => f(...a.map(v => (v === '' ? undefined : v))); 

If you want all falsy values to be substituted you can use it like this:

const N = f => (...a) => f(...a.map(v => (v || undefined))); 

If you wanted empty objects to be substituted you could use:

const N = f => (...a) => f(...a.map(v => (Object.keys(v).length ? v : undefined))); 

and so on...

Conclusion

The point is that it's your code and you know what should be the API of your functions and how the default values should work. Fortunately JavaScript is powerful enough to let you easily achieve what you need (even if that is not the default behavior of default values, so to speak) with some higher order function magic.

like image 58
rsp Avatar answered Sep 20 '22 17:09

rsp