Turn JavaScript Objects in Maps for Predictability

No more errors with undefined properties

Dev by RayRay
Feb 17 · 3 min read
Photo by NASA on Unsplash

The Problem With Objects

You’re deep into your code, and suddenly you get an error: “Cannot read property x of undefined.” Awww, man.

Most of the time, this means debugging — attacking my code with debugger here, console.log() there. Most of the time, I get it fixed fast. But there are those days that it takes me hours.

Example Problem

We have an object called car.


The car object has a set of key-value pairs. We get this information from our API. Normally, you’d get an events property inside the object that includes the property last_updated — stating the last time this information has been changed.

When running this code, we get an error.

Uncaught TypeError: Cannot read property 'last_updated' of undefined at <anonymous>:1:14

To prevent the error, we have to check if the property events is in the object or not.

This way, we don’t have an error if the event property is not in our object. But I find this code to be not that readable.

The Map Solution

When we turn our object into a Map(), we get all the clear methods from the map.

You can turn your object into a map with the Object.entries() method and give it your object as a parameter.

This would be the output of the map.

A Map() has a range of handy methods we can use to make our code more readable, predictable, and with fewer chances for getting errors.

  • set('key', 'value') store a key-value pair
  • get('key') returns the value of a key if available
  • has('key') returns true or false depending on if the key exists
  • delete('key') deletes a key-value pair from the map
  • clear() removes all the key-value pairs from the map
  • size() returns the amount of key-value pairs from the map

So if we want to have the information last_updated from the events the property, we use the .has('prop') method. This method returns a boolean based on the existence of the property.

In your console, you’ll see the console logged ‘not available’ without any errors.

As already a few other helpful developers showed in the comments. The Map isn’t maybe the best solution for every problem for making your Objects more predictable.

It turns out that the Reflect API, Optional chaining or even better, TypeScript helps to write JavaScript that is more predictable.

Still, I think it could be an interesting solution 😉


I think using Map() can make your code more readable, predictable, and less sensitive to errors. With the normal Object, your code will give errors when a certain property is missing. This will lead to bad user experience, so let's prevent this.

If you want to check out more details about Map(), please check out the detailed page of MDN web docs.

I hope you learned something new with this piece. Happy coding!

Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Dev by RayRay

Written by

I write stories on about JavaScript, Typescript, Angular, NodeJS — Subscribe to my newsletter on https://buttondown.email/devbyrayray

Better Programming

Advice for programmers.

More From Medium

More from Better Programming

More from Better Programming

More from Better Programming

Fun Side Projects That You Can Build Today


More from Better Programming

More from Better Programming

The Zero-Dollar Infrastructure Stack

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade