My task is to create a function that checks if a specific property exists in object. I don't understand why the second log returns false when 'b' obviously exists in numbers object. Would be very happy if someone can explain the solution to me :)
const existInObject = (obj = {}, prop) => {
for (const key in obj) {
if(key === prop) {
return true;
} else {
return false;
}
}
};
const numbers = {
a: 5,
b: 4,
}
const result1 = existInObject(numbers, "a");
const result2 = existInObject(numbers, "b");
console.log(result1, result2); // true, false