DEV Community


Discussion on: Hidden power of || and &&

lukeshiru profile image

I love short and clean code, but I can't get behind short-circuiting, having solutions actually designed for this scenarios, like optional chaining (?.), nullish coalescing (??), and the good old ternary (condition ? ifTrue : ifFalse).
Also, short-circuiting can bite you quite easily due to falsy values like 0, "", and so on.
You mentioned this operators a little, buy your last example should be:

const name = response?.data?.name ?? "UNKOWN";
Enter fullscreen mode Exit fullscreen mode

And should be the preferred way. The code is still simple, but the result is more predictable.

Other than that, nice post covering this "loopholes" in JS logic!


kozlovzxc profile image
Nikita Kozlov Author

Thanks! I've added a note to the post and mentioned you 🙏

patarapolw profile image
Pacharapol Withayasakpunt

Elvis cuts out not only undefined, but also null.

Still, it is much safer than short circuiting.