6 ms·
I agree with many folks here on HN in that `null` and `undefined` have important differences. `undefined` appears when you access an invalid field, while `null`
by resonious 6y ago
I agree with many folks here on HN in that `null` and `undefined` have important differences. `undefined` appears when you access an invalid field, while `null` appears when you access a _valid_, but empty field. This information comes in handy during debugging. If your code is trying to access a field on `undefined`, then you may well have a typo. If your code is trying to access a field on `null`, then you've simply made some wrong assumptions about your inputs. The next steps for those two scenarios are quite different!
Arguing that all `null`s should be replaced with `undefined`s in JS code also kind of implies to me that JSON should replace the `null` keyword with `undefined`, which just sounds crazy to me. Why store "undefined" values in serialized data? If it's not defined, don't define it... `null` allows you to convey that "this value may exist, but currently doesn't".
- dheera 6y agoAgreed, however we look at it there is no way to change the several dozen JSON libraries out there. People should avoid using undefined, not avoid using null.
- ZephyrBlu 6y agoAvoiding undefined is more preferable to me, since then it only shows when when you screw up.
- anuila 6y agoHow can you avoid using undefined if it's the default value for `prop.nonexistent` and `return`? Have you ever used JavaScript?
- afiori 6y agoone can argue that if you obtain a value in such a way you should validate it first or not use it at all
- rbanffy 6y agoFurthermore, if the function returns `undefined`, as in "it's not expected to return a value", you should probably not try to assign its return value to anything you'll see again.
- dheera 6y agoIn general I would validate for the thing I'm actually looking for instead of validating against undefined and other invalid input values. For example: function setContrast(value) { if(typeof(value) !== "number") throw "input must be a number"; if(!(value >= 0 && value <= 1)) throw "input must be between 0 and 1 inclusive"; // ... rest of code ... } This pattern will generally avoid any issues with undefined values being passed in.
- benatkin 6y agoThe key is defined but the value is not?
- elevenoh 6y agoyessir, the key is declared
- elevenoh 6y agoi think of null as conveying: "this value was intentionally set to 'null' in contrast to undefined: 'this value is undefined (e.g. was never [correctly] set)' Though this doesn't apply to dealing with unsanitized json parsing.
- Aardwolf 6y agoThere's still a distinction between explicitly assigned undefined, and deleted or not assigned field. So, two types of undefined in addition to null, if you will.
- smichel17 6y agoYes. This is annoying / I wish it weren't part of the language. It's an easy footgun, since `object.property === undefined` does not disambiguate between the two cases. Similarly, I wish `typeof null === "null"`.
- tgv 6y agoIn JS, undefined is also typical for uninitialized fields and variables, so undefined can show up without a typo. And if you've got typos, why not use e.g. TypeScript?
- afiori 6y agois there a way to use the typescript type checker on js files using jsdoc types?
- emteycz 6y agoYes. Simply install TS and make a jsconfig.json file, Vscode will pick it up automatically.
- tgv 6y agoYou need to add // @ts-check at the top of the file: https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html https://www.typescriptlang.org/docs/handbook/intro-to-js-ts....
- moron4hire 6y agoThere is a config setting for enabling it globally now https://www.typescriptlang.org/tsconfig#checkJs https://www.typescriptlang.org/tsconfig#checkJs
- afiori 6y agoI was missing this, thank you.
- mantap 6y agoIn conventional JS, undefined is used for both and null is just a curiosity. JS does make a distinction between a non-existent field and a field containing undefined as a value. Is there anywhere where the JS standard library uses null? I don't recall any. In my experience having two null values causes problems, for instance you check for one but not the other. It also remains the one remaining use of the == operator, without null we can expunge == too.
- chrismorgan 6y ago> Is there anywhere where the JS standard library uses null? Plenty, though it depends a bit on what you count as JS standard library. An object’s prototype may be null, so things like Object.getPrototypeOf(), Object.setPrototypeOf(), Object.create() and .__proto__ all deal with null. Anything that is accessing a named property that exists on a class will yield null for no value, never undefined. This is seen in DOM methods; things like Node.firstChild and Node.parentNode spring to mind as potentially null, and in each case it would be semantically quite wrong for them to be undefined—they are defined, as “no value”.
- afiori 6y agowhile I agree with your position the criticism of null can also be reformulated as: are there places in common usages where both null and undefined are common/reasonable values and have different meaning? conceptually I think so, but I am not sure how much it actually helps with the language ergonomic. in hindsight I think undefined should have been more powerful, eg setting a property to unrefined should effectively remove the property from an object (which would also affect the prototype chain)
- mantap 6y agoNot sure I agree that there is some logical reason why Node.firstChild should be null for an empty element whereas e.g. array[0] should be undefined for an empty array. I think the actual reason is just that DOM is an old API. If designed today it would return undefined.
- 6y ago
- randompwd 6y agoI see the logic being that the value hasn't yet been defined so we're going to articulate it as such. A failing of JS lang that so many of these discussions have to keep happening.
- smichel17 6y ago+1. I use null in js as the equivalent of "This page intentionally left blank."
- cookiengineer 6y agoThe underlying problem why this isn't clear to pure JS developers is that even the native APIs do this differently, but for various reasons. For example, take Array.prototype.find(). Would be nice to have null here as a default return value. But what about Arrays containing null as a value? What about the case of null || false? What about the case of false || null? There were a lot of design discussions to come up with those APIs...but I agree with you that technically, undefined should not exist. I'd rather have a syntax error being thrown than having to deal with undefined. As undefined is specified as a hackaround as a global property, ... most of the implications are just a workaround, too.