DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for 5 ways to check if the property exists in JavaScript Object
Sanchithasr
Sanchithasr

Posted on • Updated on

5 ways to check if the property exists in JavaScript Object

When I started my journey with programming, there were many problems that took time for me to understand. JavaScript Objects was one of them. However I learnt it gradually over the time after I started using it frequently. I am sure it is different topics for different people.

So today, I would like to write about how to check if the property exists in the object since it is one of the common things we deal with frequently.

Object Example

1. Using Object method hasOwnProperty()

The most common solution would be to use hasOwnProperty() which is one of the common object methods. This method returns a boolean indicating whether the object has the specified property.

var favAuthor = {
  name: 'Dan Brown',
  favBook: 'Lost Symbol',
  favCharacter: 'Robert Langdon'
}
if(favAuthor.hasOwnProperty('favCharacter')) // true
{console.log('The property exists')}
else {
  console.log('The property does not exist')
}
Enter fullscreen mode Exit fullscreen mode

2. in Operator

The in operator returns true if the specified property is in the specified object.

var favAuthor = {
  name: 'Dan Brown',
  favBook: 'Lost Symbol',
  favCharacter: 'Robert Langdon'
}
if('name' in favAuthor) // true
{console.log('The property exists')}
else {
  console.log('The property does not exist')
}
Enter fullscreen mode Exit fullscreen mode

3. Using typeof and compare it with undefined

If the property doesn’t exist, the type of the property should be ’undefined' and hence we can use typeof operator and comparing it with ’undefined' .

var favAuthor = {
  name: 'Dan Brown',
  favBook: 'Lost Symbol',
  favCharacter: 'Robert Langdon'
}
if(typeof favAuthor.name !== 'undefined') // true
{console.log('The property exists')}
else {
  console.log('The property does not exist')
}
Enter fullscreen mode Exit fullscreen mode

4. Using !! operator (double-bang operator)

This is the least known method to check the property in the object. In JavaScript, every value has an associated boolean, true or false. For example, a null value has an associated boolean value of false. A string value, such as abc has an associated boolean value of true.

You can find the list of truthy values here and the falsy values here.

Null is a falsy value.

!!null // false
Enter fullscreen mode Exit fullscreen mode

So we can take advantage of that by the name of the property we want to check using the !! operator. If the key to the property value is not null, it will return true

var favAuthor = {
  name: 'Dan Brown',
  favBook: 'Lost Symbol',
  favCharacter: 'Robert Langdon'
}
if(!!favAuthor.name) // true
{console.log('The property exists')}
else {
  console.log('The property does not exist')
}
Enter fullscreen mode Exit fullscreen mode

5. Using Optical Chaining

You can return true or false by using optical chaining. It returns undefined if the property does not exist which we can take advantage of.

var favAuthor = {
  name: 'Dan Brown',
  favBook: 'Lost Symbol',
  favCharacter: 'Robert Langdon'
}
if(favAuthor?.name != undefined) {
  console.log('Property exists')
}
else {
  console.log('Property does not exist')
}

Enter fullscreen mode Exit fullscreen mode

Comment below if you know any other ways.

Thank You

Top comments (0)

🀯

"I made 10x faster JSON.stringify() functions, even type safe"

☝️ Must read for JS devs