Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Clarify what happens for 'a ?? b' when 'a' is not defined #13

Closed
rbuckton opened this issue Oct 7, 2017 · 6 comments
Closed

Clarify what happens for 'a ?? b' when 'a' is not defined #13

rbuckton opened this issue Oct 7, 2017 · 6 comments

Comments

@rbuckton
Copy link

rbuckton commented Oct 7, 2017

Today, if you write a || b when a has not been defined you get a ReferenceError at runtime. You can test for this case using typeof a !== "undefined", which seems similar to the undefined check for ??. Is the intended behavior for a ?? b to result in b if a has not been defined, or will it still throw a ReferenceError?

@ljharb
Copy link
Member

ljharb commented Oct 7, 2017

I would expect it to still throw a ReferenceError, and I'd also expect the let/const TDZ to apply.

This isn't an error suppression operator, it's a null/undefined-based selection operator.

@rbuckton
Copy link
Author

rbuckton commented Oct 7, 2017

It's not necessarily "error suppression" in this case. The typeof operator itself proves that operators aren't required to throw a ReferenceError when accessing a variable that is not defined. I don't have a preference either way, but think it is worth clarifying the behavior in the explainer.

@alangpierce
Copy link

My reading of the spec for both this operator and the optional chaining proposal is that they throw ReferenceError in this case. They both rely on GetValue, which throws ReferenceError if IsUnresolvableReference is true. But I certainly could be misreading it, and I don't think I've seen this behavior discussed in either repo, so probably best to wait for an author to chime in.

CoffeeScript chose to treat undeclared variables the same as undefined for this operator, the a?.b operator, and the a? unary operator. It has legitimate uses (and is used in practice) on very rare occasions, like window?.foo when you don't know if you're running in a browser. So there's some precedent for making it like a typeof check, even though I very much prefer it to throw ReferenceError.

@ljharb
Copy link
Member

ljharb commented Oct 7, 2017

I think "typeof is special" is a better situation than "some ops throw reference errors and some don't".

@claudepache
Copy link

In the case of ?? and ?., I expect that you would take advantage of the “not-defined check” à la typeof only in rare situations. In most cases, you would benefit from the opposite “ReferenceError on not-defined” behaviour, because it would enable you to chase that bug early.

I could imagine a crowd of coders demanding to be able to write:

var global ??= window ?? self ?? Function('this')()

but I think they are better served with better debuggability together with:

var global = System.global

and maybe:

System.isBrowser ? window.foo : global.bar

@ljharb
Copy link
Member

ljharb commented Jan 28, 2023

Closing, since this proposal is at stage 4.

@ljharb ljharb closed this as completed Jan 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants