DEV Community

Discussion on: Clean Code Applied to JavaScript — Part III. Functions

Collapse
 
paceaux profile image
Paceaux

the one problem with default parameters in a function is that it only works if the parameter is undefined.

If the parameter is null, then the default parameter isn't applied; Default parameters don't strictly behave as a truthy check.

e.g.

function plusSome(num, some = 1) {
 return num + some;
}

plusSome(1, null); // returns 1. Because null passed in, coerced to 0
plusSome('1', null); // returns '1null'

So, even with default parameters, you still have to be careful

Collapse
 
thiagomgd profile image
Thiago Margarida

I agree in part, because generally speaking you shouldn't pass null as param, and functions shouldn't return null