2018-10-12 15:37:13 -04:00
---
title: Testing Objects for Properties
---
2019-07-24 00:59:27 -07:00
# Testing Objects for Properties
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
We do not change anything here:
```javascript
// Setup
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
```
further, in the body of the function we use `.hasOwnProperty(propname)` method of objects to determine if that object has the given property name. `if/else` statement with Boolean Values will help us in this:
```javascript
function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) == true) {
return myObj[checkProp];
}
else {
2019-07-24 00:59:27 -07:00
// and change the value of `return` in `else` statement:
2018-10-12 15:37:13 -04:00
return "Not Found"
}
}
```
Now, you can change `checkObj` values:
```javascript
// Test your code by modifying these values
checkObj("gift");
```
Here’ s a full solution:
```javascript
function checkObj(checkProp) {
// Your Code Here
if (myObj.hasOwnProperty(checkProp) == true) {
return myObj[checkProp];
2019-07-24 00:59:27 -07:00
} else {
return "Not Found";
2018-10-12 15:37:13 -04:00
}
}
// Test your code by modifying these values
checkObj("gift");
```
2019-07-24 00:59:27 -07:00
< / details >