# Check if an Object has a Property - JavaScript Data Structures

**URL:** <https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634>\
**Category:** I Built This\
**Created:** [February 21, 2023, 8:57am UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634 "2023-02-21T08:57:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![devonhughes](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/devonhughes/32/300439_2.png) [@devonhughes](https://forum.freecodecamp.org/u/devonhughes)\
**Post date:** [February 21, 2023, 8:57am UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/1 "2023-02-21T08:57:56Z")

</div>

I was able to solve [this challenge](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/check-if-an-object-has-a-property) using a for loop. The [two posted solutions](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-check-if-an-object-has-a-property/301155) utilize && operators and the every method though.

Just wondering from an efficiency point of view, if my solution is less than ideal compared to the posted solutions?

My Solution:

```auto
function isEveryoneHere(userObj) {
let arr = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  for (let i = 0; i < arr.length; i++) {
    if (userObj.hasOwnProperty(arr[i]) == false) {
      return false;
    }  
  } return true;
} 

```

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [February 21, 2023, 9:20am UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/3 "2023-02-21T09:20:05Z")

</div>

We have blurred this solution (with `[spoiler][/spoiler]` tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

---

<div class="post-metadata">

**Author:** ![cybergopnik](https://avatars.discourse-cdn.com/v4/letter/c/a183cd/32.png) [@cybergopnik](https://forum.freecodecamp.org/u/cybergopnik)\
**Post date:** [March 6, 2023, 10:31am UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/4 "2023-03-06T10:31:51Z")

</div>

How is it possible without if? I played around with @devonhughes’s code and tried to figure out how to make it with for loop only.

```auto
function isEveryoneHere(userObj) {
let arr = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  for (let i = 0; i < arr.length; i++) {
    return arr[i] in userObj;
  }
}  

```

It returns false only on Alan not being among object properties. On other names it returns true. Same story with hasOwnProp.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [March 6, 2023, 3:28pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/5 "2023-03-06T15:28:38Z")

</div>

The comment was invalid. Ignore it.

---

<div class="post-metadata">

**Author:** ![dan1294x](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dan1294x/32/255163_2.png) [@dan1294x](https://forum.freecodecamp.org/u/dan1294x)\
**Post date:** [May 17, 2023, 10:12pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/6 "2023-05-17T22:12:15Z")

</div>

here’s my solution to this. Not the most elegant but it works 🙂

```auto
function isEveryoneHere(userObj) {
  // Only change code below this line
  let user = ['Alan', 'Jeff', 'Sarah', 'Ryan']
  let check = 0;

  for (let i = 0; i < user.length; i++)
    if(user[i] in userObj == false) {
      check = 1;
    }

    return check <= 0

}

```

---

<div class="post-metadata">

**Author:** ![lopez1458](https://avatars.discourse-cdn.com/v4/letter/l/f9ae1b/32.png) [@lopez1458](https://forum.freecodecamp.org/u/lopez1458)\
**Post date:** [July 31, 2023, 10:01pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/7 "2023-07-31T22:01:38Z")

</div>

I used a for loop as well. Not the best solution but it works fine.

```auto
function isEveryoneHere(userObj) {
  let names = ['Alan', 'Jeff', 'Sarah', 'Ryan'];
  let result = '';
  for (let i = 0; i < names.length; i++) {
    if (userObj.hasOwnProperty(names[i])) {
      result = true;
    } else {
      result = false;
      return result;
    }
  } 
  return result; 
}
```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 31, 2023, 10:10pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/8 "2023-07-31T22:10:21Z")

</div>

Unlisted on July 31, 2023, 10:10pm UTC



---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 31, 2023, 10:10pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/9 "2023-07-31T22:10:25Z")

</div>

Listed on July 31, 2023, 10:10pm UTC



---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 31, 2023, 10:10pm UTC](https://forum.freecodecamp.org/t/check-if-an-object-has-a-property-javascript-data-structures/593634/10 "2023-07-31T22:10:27Z")

</div>

Closed on July 31, 2023, 10:10pm UTC


