4 ms·
In 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 unde
by mantap 6y ago
In 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.
- chrismorgan 6y ago> If designed today it would return undefined. Absolutely not. Null means it knows what you’re asking for, and there’s no value. Undefined means it doesn’t know what you’re asking for. (This includes function calls that don’t return a value, since you’re asking for a value that wasn’t produced—again different from null, which is explicitly producing a value.) Node.firstChild being null for a node with no children is absolutely correct. Undefined would be incorrect. Seeking past the end of an array finds elements that have not been defined, so undefined is correct and null would be incorrect.
- Master_Odin 6y agoExcept in these cases, undefined still can make complete sense if you're willing to rephrase things (like sindresorhus is). For your Node.firstChild example, one could say that if a node has no children, then it's firstChild is not defined as it doesn't exist, and so undefined makes sense. You can construct similar plausible arguments for other usages of null, though you cannot do the inverse.
- ZephyrBlu 6y agoNode.firstChild _is_ defined though, it's just empty. Hence null. An undefined property would be one like Node.helloWorld, which doesn't not exist at all on the object.
- chrismorgan 6y agoNode.firstChild producing null is this conversation: “Hey node, what’s your first child?” “I don’t have a first child.” Node.firstChild producing undefined is this conversation: “Hey node, what’s your first child?” “Huh? What’s a first child? I have no idea what you’re talking about.”
- dgb23 6y agoUndefined occurs for example when you don’t pass a parameter, or when a function doesn’t return a value (but you read/store it anyway). Null comes for example from JSON, from nullable SQL columns, and it is passed explicitly to say „this value is empty“ very commonly in libraries.
- bobbylarrybobby 6y agodocument.querySelector(selector_with_no_matching_elements) and other similar select methods that 1. Will only yield a single element at most (never an array) 2. Might come up empty.