3 ms·
Being explicit is great, but it doesn't prevent a library from defining it's own boolean coercion rules. The Angular team, for instance, has defined toBool() (
by spankalee 12y ago
Being explicit is great, but it doesn't prevent a library from defining it's own boolean coercion rules.
The Angular team, for instance, has defined toBool() (which they use in directives like ng-if) like this:
bool toBool(x) {
if (x is bool) return x;
if (x is num) return x != 0;
return false;
}
It treats true and non-zero numbers as true, and everything else (including null) as false.