> document.getElementsByTagName
For what it's worth I usually wrap these functions to e.g. throw an error if they don't find any items as that's what you usually want to happen but forget to write checks for. It's rare I'll use getElementById and expect it not to find something.
Lots of undefined + null values crop up from situations like this where you want to fail when something you expect to be there isn't found e.g. file handling, dictionaries, web requests.
It's a no-brainer to use TypeScript as well if you want robustness from these kinds of bugs, where returning undefined or a value is more practical because TypeScript will force you to check for undefined. I don't understand why anyone would willingly give up to type checks like these as it's obvious to me you can't compete with automated checking.
As you said,
undefined == I just don’t know what the value is
null == I know that there isn’t supposed to be a value here.
In 10 years I’ve never come across a situation where I found that distinction useful.
Null should mean in most cases that the data you wanted to access was invalid, but you can expect it to exist there in other circumstances.
Neither are a guarantee in JS, and you can find counterexamples for the counterexamples of the counterexamples, but that's the intention behind each.
const w = (fn, _) => function(...args){
return (_ = fn(...args)) === null ? undefined : _;
};
document.getElementById = w(document.getElementById.bind(document));Returning undefined for null, as well as changing dom functions is a great way to ensure unintended side effects will creep into your code.
foo
//Uncaught ReferenceError: foo is not defined
if not property access, and it lies o = { foo: undefined }
o.foo
//undefined
o.hasOwnProperty('foo')
//true
Array is an object, access by index is property access a = []
a[0]
//undefined
That's unfortunate. In Ruby: foo
#NameError (undefined local variable or method `foo' for main:Object)
defined?(foo)
#=> nil
o = Object.new
o.foo
#NoMethodError (undefined method `foo' for #<Object:0x000055f2f4e56e78>)
a = []
a[0]
#=> nil
And Ruby enforces method arity and keyword parameters: def foo(value)
end
foo
#ArgumentError (wrong number of arguments (given 0, expected 1))
def bar(value:)
end
bar
#ArgumentError (missing keyword: :value)
Basically it throws on undefined.I think reimplementation is a great way to uncover how things work, Ruby "fix" for property access
http://sergeykish.com/ruby-like-javascript-undefined.rb
Anyone knows how to relax arity?
For everything else, use false to “explicitly assign it”, you don’t need a third type.
Then you still have null, you’re just spelling it funny while confusing all the other developers who have to deal with your code and making it incompatible with language constructs like the nullish coalescing operator.
If the concept you are trying to express is “null”, then please use null to represent that instead of bringing in a poor substitute like an overloaded false.
Here are the rules I use:
* undefined is a value where a value is never before assigned. Yes, you can do stupid things in opposition to the default such as arbitrarily assigning undefined or even redefining undefined to an assignment, but those footguns are not be accident.
* null is a value that is explicitly assigned.
If you program with those assumptions in mind you are safe 99% of the time given default language constructs, which doesn't account for the intentionally bad decisions other people make. This also helps with troubleshooting because when an error is encountered due to a null or undefined value the assignment distinction helps to diagnose the problem.