Profile Lookup. Can we check for a property of "undefined"?

I’m curious as to why this code is failing the challenge? After much brain wracking I now understand why hasOwnProperty() is the correct method to use here, but I totally forgot about hasOwnProperty() until giving up on this one. I came about my first strategy by messing around with console logs trying to see what an invalid property would log and saw that it returned ‘undefined’. If the function first checks for a matching name and then checks to see if contacts[i].prop returns a value that is NOT undefined then it shouldn’t it return contacts[i][prop]? What am I missing?

var contacts = [
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]

function lookUpProfile(firstName, prop){
  for (var i=0; i<contacts.length; i++){
    if (contacts[i].firstName === firstName){
      if (contacts[i].prop !== undefined){
        return contacts[i][prop];
      else {
        return "No such property";
  return "No such contact";

// Change these values to test your function
lookUpProfile("Akira", "address");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

The problem with your code is:

if (contacts[i].prop !== undefined){

contacts[i].prop is undefined, because when you use the dot notation for an object property, you are trying to reference the property with that specific name. When you write contacts[i].prop, JS is looking for a property called “prop” in the contacts[i] object. There is no property called “prop”. Since the 2nd function argument prop is a variable, you need to use bracket notation like you do in your return statement to reference the property.

1 Like

Ok sweet! Thanks for clarifying! Hopefully I’ll remember the difference between these two notations now :slight_smile: