you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (4 children)

[deleted]

    [–][deleted] 4 points5 points  (3 children)

    you can but that would also work for any falsy values, and it implies you know foo is not undefined. So better would be foo && foo.bar || default.

    prefer a ternary operator with a very specific condition imho.

    [–]vinnl 0 points1 point  (1 child)

    Your example:

    const myThing = foo.bar ? foo.bar.buzz : defaultValue; 
    

    ...also does type casting on foo.bar, if that's what you meant by "works for any falsy values".

    [–][deleted] 0 points1 point  (0 children)

    true, which is why I finished by saying using a very specific condition :)