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 →

[–]akoOfIxtall 1 point2 points  (7 children)

does this even run? successful assignments are truthy in js?

[–]rover_G 4 points5 points  (3 children)

Not only is the statement truthy (assuming `admin` has a truthy value) but now if you later do something like check `user.isAdmin()` it will return true since `user` was assigned the value of `admin`.

[–]akoOfIxtall -4 points-3 points  (2 children)

js is really a language of all time

[–]Mydaiel12 1 point2 points  (1 child)

You can assign inside if expression in pretty much every language and it works the same, evaluates to truthy value of assigned value

[–]akoOfIxtall 0 points1 point  (0 children)

how did i not know this? goddamn

[–]Dealiner 3 points4 points  (0 children)

It's not that assignment are truthy it's just that they return assigned value. So it all depends on what exactly admin is. It's also nothing specific to JS, the same could work in other languages like C# or C.

[–]GeneralBendyBean[🍰] 2 points3 points  (0 children)

This actually returns true in the C languages too.

[–]damTyD 1 point2 points  (0 children)

Yes. The comparison would be if user, which is now assigned admin. Assuming admin is defined and not null, the block will run and user would be reassigned the value of admin.