Johannes Baum
1 min readJun 2, 2020

While you are right, that checking for falsy has a couple of drawbacks if the map contains vaues like false, 0, "", ..., using your suggested approach is also not correct for checking if an object property exist.

To check if a property exists you need to check via https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty.

Your approach of using obj['prop1'] !== undefined or typeof obj['prop1'] !== 'undefined' doesn't allow an entry of the map to have the value undefined.

Not having a straight forward interface for checking if a key exists in a map is one more reason to use Map instead of a plain object.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Johannes Baum
Johannes Baum

Written by Johannes Baum

Creator of GridEngine (https://github.com/Annoraaq/grid-engine) 👾 Software Engineer 🚀 JavaScript/TypeScript

Responses (1)

Write a response

You're right, of course, about hasOwnProperty() which also detects properties with the value undefined (whether that's useful or not) and skips properties from up the prototype chain (whether that's what you want or not).
As far as I can see…

--