Awesome article, Ifeoluwa ...
For checking that the properties of the user object exist and are valid, the following IF statement would work:
if (!!user.name && !!user.age && !!user.email && !!user.level) { // ... do stuff }
The double negation represents the property reference as a Boolean. If undefined, 0 or null, it would give false. Else, it would return true.
Boolean
undefined
0
null
false
true
It should work even when the properties do not exist.
Wow, this is so cool. I've never come across this. I knew there would be a better way.
Thanks a lot, Time Traveller :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Awesome article, Ifeoluwa ...
For checking that the properties of the user object exist and are valid, the following IF statement would work:
The double negation represents the property reference as a
Boolean
. Ifundefined
,0
ornull
, it would givefalse
. Else, it would returntrue
.It should work even when the properties do not exist.
Wow, this is so cool. I've never come across this. I knew there would be a better way.
Thanks a lot, Time Traveller :)