Well, it doesn't mean you won’t have any errors anymore. But the ting with a Map is it has a very readable API, which makes your code more readable right?

You are completely right that it can be done with if(obj.event) . But if you expect an event is an available and inside event there is a property, you will have to check it like: if(obj.event && obj.event.prop) which is in my opinion not so readable.

I personally prefer to read it like this: if(obj.has('event')) or we use the Reflect APIif(Reflect.has(obj, 'event')) . Or we could use optional chaining, but the browser support is not that high at the moment.

My story was not a about what is better, but my opinion about readability and predictability in our code when we deal with Objects.

Written by

I write stories about Frontend Dev, JavaScript, Typescript, Angular, NodeJS, Serverless Functions, JAM Stack, FaunaDB, Netlify, Apple, iOS— https://byrayray.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store