|
|
|
|
|
by lioeters
232 days ago
|
|
Apparently NaN (not a number) becomes false when type-cast to boolean. Boolean(NaN)
===> false
For a hypothetical NaB (not a boolean), the same behavior seems logical. Boolean(NaB)
===> false
So the condition `if (NaB)` is false and will fall through to the `else` branch. But..> what you do determines what NaN === NaN actually evaluates to I think I disagree with this because it's not about casting to boolean, it's a totally different question of self-identity, or comparing two instances (?) of a value (?!). From the article: typeof NaN
===> "number"
For symmetry and consistency: typeof NaB
===> "boolean"
> NaN is the only value in the whole of JavaScript that isn’t equal to itself .. the concept of NaN is meant to represent a breakdown of calculationSimilarly, NaB would represent a breakdown of true/false condition (somehow) as an exceptional case. Whether it equals itself is a matter of convention or language design, not logic - since it's beyond logic just as NaN is beyond numbers. I would say: NaN === NaN
===> false
NaB === NaB
===> false
> you throw an exception (and imo it is better..I agree throwing an exception is better design for such exceptional cases - but we know JavaScript as a cowboy language would rather plow through such ambiguities with idiosyncratic dynamic typing, and let the user figure out the implicit logic (if any). |
|
In your examples, it does not make sense to have both
and But maybe if you had NaB it would make sense to evaluate And maybe also evaluate `NaN === 1` to NaB?I am not too fond of NaB because boolean is supposed to encode binary logic. There exists ternary logic and other concepts that could be used if you do not want strictly two values. Or if you want to encode exceptional values, no reason not to use int8 directly, instead of calling it boolean but actually using sth that could be represented as int8 (NaB has to be represented by some byte anwyay). In general, tbh, I think often it is not useful to encode your logic with booleans because many times you will need to encode exceptional values one way or another. But NaB will not solve that, as it will just function as another way to encode false at best, throwing exceptions around at worst.
> Similarly, NaB would represent a breakdown of true/false condition (somehow) as an exceptional case.
Still, in JS `NaN || true` evaluates to `true` hence I assume `NaB || true` should evaluate to true too. It is not quite the same as NaN + 1 evaluating to NaN. And as NaN (and hence NaB) functions as false when logical operations are involved for all intents and purposes, except if you exactly inquire for it with some isNaB() or sth, to me NaB is another way to encode false, which is probably fine depending what you want it for.