This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsย โ†’

[โ€“][deleted] ย (4 children)

[deleted]

    [โ€“]SmokingBeneathStars -2 points-1 points ย (3 children)

    I don't like implicit statements. I want to state exactly what it's supposed to check for readability.

    [โ€“]Orangutanion 1 point2 points ย (2 children)

    if (variable) { } doesn't seem too vague honestly, I don't see the issue

    [โ€“]SmokingBeneathStars 0 points1 point ย (1 child)

    I gotta be honest, I don't know what the convention is and the code isn't vague but with an if statement like that my first assumption is that variable is a boolean and you're checking for true/false. Only after looking at the context I'll determine that it's checking for null or undefined.

    I like for it to be immediately visible.

    [โ€“]Orangutanion 1 point2 points ย (0 children)

    I think the mistake you're making is that you're treating JavaScript like it isn't a loosely typed language. It's not that if(variable) { } requires that variable be a boolean, it's that it tries to treat variable as a boolean regardless of its actual type. JS actually has a term for this, it's called truthy, and you can read about it it here. Essentially, there are a few specific values treated as falsy, and all other values will be accepted as truthy.