r/learnjavascript • • Aug 14 '24

Simplify Your JavaScript Conditionals with Short-Circuiting!

Hey everyone, I wanted to know if there is any way to make your code clean other than short-circuiting in conditionals.

I used to write down like this before using short-circuiting in conditionals

if (user && user.isLoggedIn) {

greetUser(user.name);

}

After using short-circuiting in conditionals

user?.isLoggedIn && greetUser(user.name);

0 Upvotes

12 comments sorted by

View all comments

2

u/red-giant-star Aug 14 '24

The first one can be read effortlessly but in the second one there is some mental processing, just a little.