DEV Community

Cover image for You HAVE to know the new question mark syntax in ES 2020
Jaden Concord
Jaden Concord

Posted on

You HAVE to know the new question mark syntax in ES 2020

I just discovered the Javascript feature that I have been waiting for in a video by FireShip.
You know how dangerous it is to not prevent an error to something like this,

function getDataFromClient(data){
  console.log(data.items.name);
}
Enter fullscreen mode Exit fullscreen mode

If the above were a node.js application, the client sent a response as an empty array, the server would simply cause an error and stop. Dangerous!
You could solve the problem like this

data.items && data.items.name
Enter fullscreen mode Exit fullscreen mode

but if you forget to do that it is super dangerous.

Now, in ES2020, there is a new feature that allows you to easily prevent this. It looks like this

data.items?.name
Enter fullscreen mode Exit fullscreen mode

Watch this video to know more about it.
Alt Text

Top comments (0)

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay