Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I use null in data because Json.

But otherwise I just leave undefined to play the role of "this optional argument wasn't defined" and null for everything else. I like null because You have to specifically set something as null. I can run into undefined through typos and such. I dont think you can accidentally hit null.

P.s. This reminds me of the one time in Python I thought, "Damn I wish I had both undefined and None". I needed to know if a user was omitting an optional argument or passing an explicit None into it. Ie. This data is optional and the data might be "None". I ended up using kwargs which weakened my function as it nolonger described all the possible arguments.



> I needed to know if a user was omitting an optional argument or passing an explicit None into it.

Sounds like kindof like an Optional<Optional<Value>>. An optional is just a container of at most one element, so you could emulate that with a singleton list, having None mean not specified, [value] be value, and [None] be explicit lack of value.


The solution in Python is to create a sentinel value/object. You can even call it undefined: `undefined = object()`. `None is undefined` ==> false


If you care about whether an optional argument is omitted or not in Python I would default it to false instead of None.


What about boolean optional arguments?


Undefined is “stored” the same way in JSON, which means it isn’t. This only becomes a “problem” when you convert a JSON string to an object and the data contains an array with a null value in it. That’s the only case where null exists in JSON, and I think it’s generally pretty rare.


> Undefined is “stored” the same way in JSON, which means it isn’t.

Null is:

  >> JSON.stringify({"foo": undefined});
  "{}"
  >> JSON.stringify({"foo": null});
  "{\"foo\":null}"


The point is, why use it at all if both values are equivalent to "missing"? I don't see a practical difference between undefined and null in the vast majority of cases, which is the the author is arguing.

Again: In the vast majority of cases. I'm sure you'll find your exceptions.


I've worked with a ton of APIs where these two aren't equivalent.

Consider partial object updates. "null" means "explicitly set a field to null", whereas undefined means "don't change the value of the field". The distinction is meaningful.


In the first case I lost the information that the key exists and can contain a value.

The second form makes it explicit.

On the first case, the parser that's getting the data may want to calculate a sensible value. On the second one, it's clear the value is known.


Not sure I follow. JSON has null. It doesn't have undefined.


You said

> But otherwise I just leave undefined to play the role of "this optional argument wasn't defined"

And the reply is saying that's how JSON uses it too. Anything set to undefined will not be serialized, as if it were optional and not defined.


Arguments belong to functions.

Sorry I was wandering all over.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: