• bleistift2@sopuli.xyz
    link
    fedilink
    English
    arrow-up
    28
    ·
    1 day ago

    The logic is fine. If you rename the variable to isAdmin, it makes perfect sense. Either they are an admin, or they are not an admin, or the state is unknown (here expressed as null). If you want to throw another JS-ism at this, undefined could be assigned before the check has been made.

    I regularly use variables like this. If users is undefined, I haven’t fetched them yet. If they’re a list, then fetching is complete. If they’re null, then there was an error while fetching.

    • HelloRoot@lemy.lol
      link
      fedilink
      English
      arrow-up
      26
      ·
      edit-2
      1 day ago

      The only flaw is that the console.log states that null means user is not logged in.

      If there are three or more explicit states, you should not use a nullable bool, but some more explicit data structure, like enum.

      For example, if the state comes from a db, the user could be successfully logged in, but somehow for a range of possible reasons this variable ends up as null and you’ll have a hell of a time debugging, because the log will give you nonsense.

  • potatoguy@potato-guy.space
    link
    fedilink
    arrow-up
    19
    ·
    1 day ago

    Today i have seen:

    if (var === true || var === ‘true’ || var === “true”)

    I’m just fortunate enough to not work with the frontend at our very backend service, but I always hear things that shouldn’t be even allowed in this planet.

  • pineapple@lemmy.ml
    link
    fedilink
    English
    arrow-up
    2
    arrow-down
    1
    ·
    19 hours ago

    I’m so confused. I understand the reasoning behind two equal signs but three?

    • Nailbar@sopuli.xyz
      link
      fedilink
      arrow-up
      8
      ·
      edit-2
      19 hours ago

      Afaik with three it also requires the types to be equal. Like 0 == false, but 0 !== false, because they’re different types.

    • Nailbar@sopuli.xyz
      link
      fedilink
      arrow-up
      2
      ·
      19 hours ago

      The === is the best! I want to know they’re really the same and not just evaluate the the same.