2018-10-10 18:03:03 -04:00
---
id: 587d7b7d367417b2b2512b1c
2021-02-06 04:42:36 +00:00
title: Check if an Object has a Property
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:14:21 +08:00
forumTopicId: 301155
2021-01-13 03:31:00 +01:00
dashedName: check-if-an-object-has-a-property
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the `hasOwnProperty()` method and the other uses the `in` keyword. If we have an object `users` with a property of `Alan` , we could check for its presence in either of the following ways:
2020-08-04 15:14:21 +08:00
```js
users.hasOwnProperty('Alan');
'Alan' in users;
2021-02-06 04:42:36 +00:00
// both return true
2020-08-04 15:14:21 +08:00
```
2020-12-16 00:37:30 -07:00
# --instructions--
2021-02-06 04:42:36 +00:00
We've created an object, `users` , with some users in it and a function `isEveryoneHere` , which we pass the `users` object to as an argument. Finish writing this function so that it returns `true` only if the `users` object contains all four names, `Alan` , `Jeff` , `Sarah` , and `Ryan` , as keys, and `false` otherwise.
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
The `users` object should only contain the keys `Alan` , `Jeff` , `Sarah` , and `Ryan`
2020-12-16 00:37:30 -07:00
```js
assert(
'Alan' in users & &
'Jeff' in users & &
'Sarah' in users & &
'Ryan' in users & &
Object.keys(users).length === 4
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The function `isEveryoneHere` should return `true` if `Alan` , `Jeff` , `Sarah` , and `Ryan` are properties on the `users` object
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(isEveryoneHere(users) === true);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The function `isEveryoneHere` should return `false` if `Alan` is not a property on the `users` object
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
delete users.Alan;
return isEveryoneHere(users);
})() === false
);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The function `isEveryoneHere` should return `false` if `Jeff` is not a property on the `users` object
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
(function () {
delete users.Jeff;
return isEveryoneHere(users);
})() === false
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The function `isEveryoneHere` should return `false` if `Sarah` is not a property on the `users` object
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(
(function () {
delete users.Sarah;
return isEveryoneHere(users);
})() === false
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The function `isEveryoneHere` should return `false` if `Ryan` is not a property on the `users` object
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
(function () {
delete users.Ryan;
return isEveryoneHere(users);
})() === false
);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:14:21 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
// Only change code below this line
// Only change code above this line
}
console.log(isEveryoneHere(users));
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
return [
'Alan',
'Jeff',
'Sarah',
'Ryan'
].every(i => obj.hasOwnProperty(i));
}
console.log(isEveryoneHere(users));
```