DEV Community

Discussion on: Discovering JavaScript: lets, vars and ifs

Collapse
 
mykeels profile image
Backlog Slayer

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.

It should work even when the properties do not exist.

Collapse
 
iifeoluwa profile image
Ifeoluwa Arowosegbe

Wow, this is so cool. I've never come across this. I knew there would be a better way.

Thanks a lot, Time Traveller :)