JS && Operator Quirks: Better Approaches?
Tags: react, typescript, beginners, codenewbie
Published: 2024-01-01 06:47:42

Hello everyone! It's been a while, and I hope you're all doing well. Reflecting on the past year, I'm thrilled about the strides I made in my writing journey, sharing insights and experiences through my blogs. Here's to more growth and compelling stories in the coming year! Also Happy New Year!

---

Without further ado lets roll with the title above, Recently on the internet I saw [Theo](https://twitter.com/t3dotgg) explaining what's wrong with the using of `&&` operator in js while returning inside of a functional component.

Let me explain, What I mean't above by my code :

```
 export default function App() {
  const people = ["deliSrey", "deli", "Srey"];
  return (
    <div className="App">
      {people.length
        && people.map((_people, index) => {
            return <li key={index}>{_people}</li>;
          })}
    </div>
  );
}

```
It result's something like this:

![code-snippet-result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oezqi5t62ny85byox65m.png)

---

> You might think What's wrong in it There is nothing wrong in it, but also there is something not correct about It, here it could result in a potential bug if you see in a hindsight 

For Example If there is nothing inside the people array list and hence the people.length is 0 which by default returns 0 
rendered on the screen something like this, Here is the code and result attached:


![code-snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y92c5zg5cg4eqqd5ll6l.png)

![code-snippet-result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g51gk3fs75v7cfs7qlf1.png)

---

Above is how ternary operator replaced by `&& operator` looks like, which almost make sense but not in case of nested ternaries:

---

With Nested Ternary :

![code-snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7sm05cmkrkjfryxno0w.png)

---
 
> But now What should we do instead, Should we go towards ternary operator, I mean it is a praticed way but in the same video `Theo` explained an interesting way to do it. When there are way more ternary operators nested for checking out multiple conditions, which create a bitter mess and the code is almost unreadable, we can break it into Components,That's where the beauty of React lies, split the nested stuff into the functional components 

here's how it is :

![code-snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8qwgqkytqpf9z0yugyb.png)


![code-snippet-result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl977hnzuizaqudj0sp9.png)

---

In case where the people array list is empty :

![code-snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqx9gz2dyakgm2m3e6bw.png)


![code-snippet-result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ckg5w9cmmbiufp97ttlv.png)

Here you can see how the readability and flow of the code is maintained more accurately and hence while working in a team It makes perfect sense to maintain the code quality where the code can be easy to debug and can be easily improved with new features.

That's all in this blog, I guess If you have already came up til here, You might also try smashing your thumb on follow and like button Trust me it helps my blog to reach more people like you and me!

Also Special Thanks to the `W` Engineer [Theo](https://twitter.com/t3dotgg) For putting this in his video which inspired me to write a blog on it as well also here is the link to the [video](https://youtu.be/mOwZhb9bZ5s?feature=shared)  

Until then it's a huge goodbye fellas!