-
Notifications
You must be signed in to change notification settings - Fork 23
Is there also a nullish AND? #61
Comments
You can write: In any case, duplicate of #4. |
Well but I would need to write condition1 not equal null and not equal undefined and condition2 not equal null and undefined... which is cumbersome |
Which is exactly what |
Sorry my bad you are absolutely right, I am so used to !== that I didn't spot that. Thanks! |
The issue is that |
Hi @ExE-Boss that is very interesting, this is the first time I hear that |
Annex B § The // This is actual code that exists on old webistes:
if (document.all) {
// Internet Explorer code
} else {
// NetScape Navigator code
} |
@ExE-Boss Hmm... Do you know why this behavior does not appear directly in the Abstract Equality Comparison spec though? |
It’s in annex b, which is legacy requirements for web browsers, which currently is not inline. |
Nice! Thanks everyone, sorry if I drifted the topic off a little bit. |
Babel could use an operator like this: babel/babel#11248 (comment) |
I'm glad to see this isn't closed. In addition to the above concerns about edge cases in the exact meaning of |
Note that this proposal is at stage 4, which means that there will be no further changes beyond bugfixes. Any new feature will be part of another proposal. (To the maintainers of this repo: Is it time to do some housekeeping and close most open issues?)
We can either adapt coding standards and linter rules so that they allow us to use an existing feature of the language, or duplicate an existing feature of the language so that we can circumvent coding standards and linter rules. Personally, I tend to prefer the first option.
I’m not sure that |
Welp, that's that, I guess. Makes sense, and certainly this isn't nearly as important as the
The coding standards and linter rules are there because loose equality is a footgun, and the language feature is used wrong by accident more often than it is used right on purpose. (Reasonable people can disagree about whether this is in fact true.) We could update the standards/rules to say "except when it's loose-equals null because you meant 'null or undefined', as long as you did it intentionally and know what you're doing", or we could add an operator that succinctly and explicitly means "I am checking for null or undefined".
Me too; I picked the first special character I could think of that I was pretty sure didn't have an existing meaning -- naming things is of course one of the Hard Problems. In #4,
It would be trivial to write a function, but you could say the same thing about |
I agree. In codebases I work on we use linter rules to enforce tripe equal for anything but null/undefined comparison. Using We also have type guards such as In my eyes, adding a special operator for a case which already has a perfectly valid and concise solution is superfluous and confusing. |
So, you were also against the nullish operator that did get in, |
No I wasn't since // Before with ternary
console.log(data.nickname != null ? data.nickname : (data.name != null ? data.name : 'N/A'));
// Before with functions
console.log(getDefault(getDefault(data.nickName, data.name), 'N/A'));
// Now
console.log(data.nickname ?? data.name ?? 'N/A') |
function getDefault() {
for (let a of arguments) {
if (a !== null && a !== undefined) { return a; }
}
}
getDefault(data.nickName, data.name, "N/A"); That is, IMHO, exactly as "readable" as All I'm saying is, if we're going to add sugar, let's at least be consistent. My point above was that |
Oops: both of our That leaves us with the ternary operator for comparison. I believe it's about as verbose in the "AND" case as with the "OR" case: x = data.nickname != null ? f(data.nickname) : (data.name != null ? f(data.name) : 'N/A'));
x = (data.nickname ?& f(data.nickname)) || (data.name ?& f(data.name)) || 'N/A'; To me, the latter (hypothetical) version conveys intent better and avoids the complexity of nested ternary operators. |
My requirements for a replacement of
Applying DRY, I short-circuit what I was going to write and backlink to #4 (comment). |
Do you have a list of languages that have "nullish AND"? |
I don't know of a similar feature in other languages, but then the first I heard of I agree that at this point it's looking more and more like a shorthand for |
I second #61 (comment) - since if we add the sugar for |
Anyway, in my opinion, |
It seems that in many of the examples for this operator, the value ends up being repeated in RHS name = data.name != null ? capitalize(data.name) : 'N/A'
name = (data.name ?& capitalize(data.name)) ?? 'N/A' which makes me wonder if this use case was actually better served by the suggested optional chaining support in the pipeline operator proposal (pipeline proposal, optional chaining suggestion). name = (data.name ?> capitalize) ?? 'N/A' |
I guess many of the popular other languages out there are typed by default - so that the need for such an "nullish AND" didn't really arose. When dealing with data where |
Is it too late to propose an alternative syntax? null ||| true
//returns true
0 ||| true
//returns 0
null &&& true
//returns false
0 &&& true
//returns true
null ?? true : false
//returns false
0 ?? true : false
//returns true |
Yes it is. Nullish coalescing is stage 4 and already implemented by browser vendors. See https://tc39.es/process-document/ for details about proposal stages. |
Too bad. Thanks for the timely reply @MatthiasKunnen |
Not sure if you mean the same thing. The syntax for nullish coalescing is indeed fix - but for nullish AND as per this thread it's still completely open and up for discussion wether this should be a thing at all |
Closing, since this proposal is at stage 4. |
The nullish coalescing acts like a logical OR where it returns the left part when it's not null or undefined.
Is there also an easy way for an AND where it returns the right?
Typically by chaining AND conditions one expects "the program to run from left to right and stop + return" if a condition is not met. Normally falsey values, with nullish coalescing only NIL (null + undefined)
I would like to achieve an AND chain like so:
But it should not stop on falsey but only on null/undefined.
Is there an easy way?
Doesn't feel right.
The text was updated successfully, but these errors were encountered: