3 ms·
> When trying to define default values, people get confused with expressions like a === b ?? c and assume it will be parsed as a === (b ?? c). When in actuality
by thaumasiotes 3y ago
> When trying to define default values, people get confused with expressions like a === b ?? c and assume it will be parsed as a === (b ?? c). When in actuality it will be parsed as (a === b) ?? c.
Note that this is pointing out a bug in the language spec. There are two ways to assign the precedence:
a === b ?? c
One says "Compare a to a value, b, which might be null. If it is null, compare against the guard value, c, instead."
a === (b ?? c)
The other way says "Compare a to b. Consider whether the result of the comparison is NULL, and if it is, do nothing. If it isn't, do nothing then too. In all cases, return the comparison between a and b."
(a === b) ?? c
There's a good reason people make the erroneous assumption that the language itself isn't trying to sabotage them. The left operand of ?? must be a nullable value; for === to bind tighter than ?? implies that it can return NULL, which in fact it can't.