DEV Community

Berra
Berra

Posted on

1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more