Hacker News new | ask | show | jobs
by pwdisswordfish 3240 days ago

    let a = 0
    let b = 123
    let result = a || b
sets result to 123, but 0 !== null.
1 comments

EDIT: Rereading parent comment I realize you already know all the below, but maybe parent doesn't quite?? Original comment: That's kind of fundamental expectation within JavaScript, though: 0 is a falsy value. The || doesn't check for null, it coerces the first expression to a boolean, and if it is true, returns the first expression. If it is false, then it returns the second expression (if I recall correctly!). If 0 being falsy is a problem, then the || might not be the easiest/clearest way to do what you need. Since you'd end up needing to nest some separate check for zero on the first expression. In which case you could just handle all your logic in that check.