Spyke
sopuli.xyz

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.

32
HelloRootreply
lemy.lol

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.

29

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.

25

the fun/horrible thing about typescript is you can just start doing js stuff as any time you like...

2
Nailbarreply
sopuli.xyz

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

2

any sane language has == behave that way. Javascript == is just completely bonkers.

3

Aren't the first two if branches completely useless? The variable was just defined without a value, so it should always be null

2
lemmy.ml

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

2

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

9

You reached the end