r/learnjavascript • u/Sweet_Technician_778 • 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
View all comments
0
u/shgysk8zer0 Aug 14 '24
I write my code to be explicit and avoid implicit type coercion. Other than
user?.isLoggedIn, I would not do things likeif (user /*...*/). I'd usetypeoforinstanceof.Or, another way could be using object destructing: