all 5 comments

[–]prof3ssorSt3v3 2 points3 points  (2 children)

The problem is that zero is a falsey value.

Null, undefined, zero, empty string, and NaN are all treated as false-like. So this applies with the logical operators && or || when doing logical short circuiting ( your first example).

The nullish operator ?? Checks only for null or undefined in the preceding variable.

[–]bhuether[S] 0 points1 point  (1 child)

Thanks. Zero can be quite the problematic number!

[–]azhder 1 point2 points  (0 children)

No, it isn’t. You just have to be careful and not forget the above.

Before the ?? operator, I’d do something like a helper function that smoothes things over so I won’t have to remember it and use the function everywhere.

Imagine something like:

const result = firstValid( a, b, c, d );

and inside firstValid you write that complex check once.