4 ms·
Assuming assert existed, it would almost certainly be judging its value for being falsy, while the ?? operator judges its LHS for being nullish, which is a narr
by kbolino 11mo ago
Assuming assert existed, it would almost certainly be judging its value for being falsy, while the ?? operator judges its LHS for being nullish, which is a narrower category. For strings, this affects whether the empty string is acceptable or not.
- fainpul 11mo agoconsole.assert(maybeNull != null, 'variable not set')
- deleted 11mo ago[deleted]
- kbolino 11mo agoFair, this is a good example of when != is actually the right choice (instead of !==). However, on web browsers, this will log an assertion failure to the console, but it will not throw an exception. It's also not suitable for the original context, where the non-null value was extracted into a variable.
- xscott 11mo ago> Assuming assert existed It's a function you could write for yourself and give it whatever semantics you feel is best. No changes to the language required for this one.
- deleted 11mo ago[deleted]