DEV Community

Berra
Berra

Posted on

Optional length?.

Imagine you have an array of news item. And if that array is empty you do not want to render your news list. Of course!

And maybe. Your api in that case does not give you the "news" key at all. And you have to write code like this:

if(!news || news.length === 0) return null;
Enter fullscreen mode Exit fullscreen mode

Then I have news for you!

Use the new optional chaining syntax.

if(news?.lenght === 0) return null;
Enter fullscreen mode Exit fullscreen mode

So clean!

And remember how to spell length.

Until next time!

Top comments (0)