Explain Codes LogoExplain Codes Logo

How to check if object property exists with a variable holding the property name?

javascript
dynamic-property-names
javascript-best-practices
property-checking
Alex KataevbyAlex Kataev·Dec 18, 2024
TLDR

To determine if an object has a property with the name stored in a variable:

  • Use in for checking the entire prototype chain:
let prop = 'key', obj = { key: 'value' }; // I'm your property 'key' let exists = prop in obj; // obj whispers: "I have the key prop!"
  • Use hasOwnProperty() to scan only the object itself, excluding the prototype chain:
let prop = 'key', obj = { key: 'value' }; // This time, it's personal let exists = obj.hasOwnProperty(prop); // obj: "It's a yes from me, dawg"

Although hasOwnProperty() gives a direct inspection, choosing in brings advantages like flexibility with variable property names, and dodging the bullet if hasOwnProperty gets overridden.

Dynamic property names handling

In the wacky world of dynamic property names, dot notation won't cut it. Instead, you want to use bracket notation which can put variables straight into the access syntax, turning properties into wild, spontaneous creatures:

let propertyName = 'dynamicKey'; // Dynamic property, like jazz let exists = propertyName in obj; // obj: "I'm feeling spontaneous today"

Have a nested object? You can still check for the property hidden deep within:

let nestedProp = 'innerKey'; // Deeper... let exists = obj[nestedProperty] && nestedProperty in obj[nestedProperty]; // obj: "You've reached down to my soul, man"

Insights on property checks

While checking properties is mostly a breezy walk in the park, watch out for a few nasty potholes. For instance, hasOwnProperty() might flop if it's overwritten by some prankster. Here's a workaround:

let exists = Object.prototype.hasOwnProperty.call(obj, prop); // Playing it safe

eval function might lure you in for dynamic property checks, but resist! It's a security risk. Stay on the straight and narrow with bracket notation and in.

Checking property existence and beyond

You can gear up typeof and bracket notation to check not just existence but also verify data type:

let typeCheck = typeof obj[prop] !== "undefined"; // obj: "I've got substance, man"

And let's not forget the difference between something existing and it being truthy. Existence doesn't always equate truthiness:

let property = 'optionalFeature'; if (obj[property]) { // Property exists and is truthy! Party time! } else { // Property doesn't exist or it's not feeling like partying (falsy) }

Always quote property names when using hasOwnProperty or the in operator. Your future self (and your code) will thank you.

hasOwnProperty vs in - Battle of the titans

Your choice between hasOwnProperty and in should depend on what you're looking for:

  • hasOwnProperty is your guy if you don't want to deal with prototype's properties.
  • Go for in if inheriting properties or variable property names are part of your game plan.

If hasOwnProperty() has been overridden, check it on the prototype instead:

let exists = Object.prototype.hasOwnProperty.call(obj, prop);

Keeping these distinctions in mind will save you from potential property-checking mishaps.