this post was submitted on 15 Oct 2024
10 points (100.0% liked)

JavaScript

1700 readers
1 users here now

founded 2 years ago
MODERATORS
 

Should I create functions/methods for packages/libraries that allow optional parameters to accept null?

In this example below, I set the 3rd and 4th parameter as null which will act as the default value.

myLibrary.myFunction(1, 7, null, null, true);

Or is this not a good way to go about creating functions for a package and therefore should not accept null as a parameter value.

myLibrary.myFunction(1, 7, false, 4, true);
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 8 points 1 month ago

My recommendation for APIs that have optional parameters is to have an optional last options parameter, every property of which can be left unset. Makes it far clearer to the caller what those properties do.