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)