I'd like to know the Double if

Tell us what’s happening:

Hi Friends,

I have a big question :-). Can please someone explain me the double if ? Until now I know just the single if.

Ps: I’m a beginner :slight_smile:

Thank so much in advance.

 if (contacts[x].firstName === name) {
        **if (contacts[x].hasOwnProperty(prop)) {**
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}

Your code so far


//Setup
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"]
    }
];

If statements can be used anywhere in a code - you can nest loops, you can nest if statements

In this case the outer if is checking if the name match (if it is the right contact) and the inner one if there is the required property

Thank you very much, very kind.

If I understood good, the second if is just a check if something exist?

Thanks

The block of code of the if statement execute only if the condition is true - in this case the outer if is used to check if the current evaluated object is the right one (if it is the contact with that name then the other if statement is executed) the inner if statement is used to check if the object has the required property

Now I understand, very very kind.

Now I know something more…

bye