Handling the distinction between undefined- and null-parameters in JavaScript

Posted by Jakob on Stack Overflow See other posts from Stack Overflow or by Jakob
Published on 2010-06-02T16:48:18Z Indexed on 2010/06/02 16:54 UTC
Read the original article Hit count: 184

Filed under:

I know very well that null and undefined are distinct in JavaScript. However, I can't seem to decide whether or not use that fact when my own functions are passed one of those as its argument.

Or, expressed in a different way, should myFoo(undefined) return the same thing as myFoo(null) or is everything fine if it doesn't?

Or, in yet another case, since myBar(1, 2, 3) is the same thing as myBar(1, 2, 3, undefined, undefined), should myBar(1, 2, 3, null, null) return the same thing as myBar(1, 2, 3)?

I feel that there's potential for confusion in both cases and that a library should probably follow a convention when handling null/undefined.

I'm not really asking for personal opinions (so please express those as comments rather than answers). I'm asking if anyone knows if there is a best practice that one should stick to when it comes to handling this distinction. References to external sources are very welcome!

© Stack Overflow or respective owner

Related posts about JavaScript