JavaScript check if property exists in Object

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Glad you enjoyed it Rakesh
Great Article! In the conclusion I'm pretty sure you mean "in check" and not "on check". Might wanna check that out to avoid confusion.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

You might need to determine if an object holds a particular property.
Let's say we have a user object. Optionally the email property can be set. If not, we want to show a form so the user can fill out their email.
How can we determine if this field exists?
const userOne = {
name: 'Chris Bongers',
email: '[email protected]',
};
const userTwo = {
name: 'John Do',
};
And to answer that, there are several ways of doing that. Let's take a look at the three most common ones.
By using hasOwnProperty we can evaluate if an object has Own property.
Let's see how it would work on our example data.
console.log(userOne.hasOwnProperty('email'));
// Returns: true
console.log(userTwo.hasOwnProperty('email'));
// Returns: false
That is perfect. But there is a catch to using this method. It only works for Own properties, not extended object properties.
As you may know, objects come with the toString method, and if we try to check if that exists, it will return false. (While this does exist)
console.log(userOne.toString());
// Returns: [object Object]
console.log(userOne.hasOwnProperty('toString'));
// Returns false
Another more explicit way of checking if an object has a property is using in.
This one can check in own and inherited properties.
console.log('email' in userOne);
// Returns: true
console.log('email' in userTwo);
// Returns: false
console.log('toString' in userOne);
// Returns: true
The last method is to use an undefined check. This method will work for omitted properties but can cause you headaches if the property exists but has an undefined value.
console.log(userOne.email !== undefined);
// Returns: true
console.log(userTwo.email !== undefined);
// Returns: false
console.log(userOne.toString !== undefined);
// Returns: true
Now let's see what happens in the following example:
const userThree = {
name: 'Steve Stevenson',
email: undefined,
};
console.log(userThree.email !== undefined);
// Returns: false
The check is acceptable, but it's not what we might be looking for.
When trying to find out if an object holds a particular property, we need to consider how safe we want to be.
I would generally not recommend using the undefined check.
If you only evaluate Own properties, the hasOwnProperty is a solid solution.
But you might want to be on the safe side and use the in check to determine if an object has a property.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter