I started learning JavaScript. I try to be not the guy who just solves the tasks, furthermore I would like to understand what is going on in JavaScript. Therefore I have two basic questions:

One Example has the following code ( I simplified it):

function phoneticLookup(val) {
var result = “”;
var lookup = {
1: “Adams”,
2: “Berta”
};
result = lookup[val];
return result;
}

Is my understanding correct that, in real world, this function requires an user input (user has to input the property-number)? Then the function lookups the value (val) and binds it on “result”?

Another example is like this:
var myObj = {
gift: “pony”,
pet: “kitten”
};
function checkObj(checkprop) {
if (myObj.hasOwnProperty(checkprop) == true) {
return myObj[checkprop];
} else {
return “Not found”
}
}

Here I would like to know if the “syntaxisation” (I don`t know if it is called like this…) leads to let the function return the value of the property.

Means:

if(myObj.hasOwnProperty);
----- would checks for true or false

if(myObj.hasOwnProperty(checkprop) == true);
------- Does the “(checkprop)”-part refer, in case of condition is true, to the value of the property?

Yes, you input a value to the function, that value is assigned to val, it is used to look up the actual value in the lookup object, then that value is assigned to result and returned. Note, it’s not really ‘user input’, it’s just passing a value to a function.

It can be simplified, the var result = "" then result = lookup[val]; return result; is just there to make the steps involved obvious. Just to cut the amount of code in my examples a but, I can simplify to:

function phoneticLookup(val) {
var lookup = {
1: 'Adams',
2: 'Berta'
};
return lookup[val];
}

So say you call the function like phoneticLookup(1).

function phoneticLookup(1) {
var lookup = {
1: 'Adams',
2: 'Berta'
};
return lookup[1];
}

Then lookup[1] is 'Adams', so it becomes

function phoneticLookup(1) {
return 'Adams';
}

Um sort of

So SOME_OBJECT.hasOwnProperty(SOME_PROPERTY) is a function: on some object, see if it has some property - return true if it does, false if it does not.

So for

var myObj = {
gift: “pony”,
pet: “kitten”
};

If I do myObj.hasOwnProperty('gift'), it does have a property called 'gift', so that is true.

If I do myObj.hasOwnProperty('blah'), it does not have a property 'blah', so that is false.

So substituting values into this function:

function checkObj(checkprop) {
if (myObj.hasOwnProperty(checkprop) == true) {
return myObj[checkprop];
} else {
return 'Not found';
}
}

Say I call the function like checkObj('gift').

function checkObj('gift') {
if (myObj.hasOwnProperty('gift') == true) {
return myObj['gift'];
} else {
return 'Not found';
}
}

Substitute again:

function checkObj('gift') {
// myObj *does* have a property 'gift'
if (true == true) {
return myObj['gift'];
} else {
return 'Not found';
}
}

Substitute again

function checkObj('gift') {
// True is true, so the bit of code in this condition executes:
// if (true == true) {
// myObj['gift'] is 'pony'
return 'pony';
// This is ignored
// } else {
// return 'Not found';
// }
}

Note that if...else is saying "if this condition is true, execute the code in this block, else execute the code in the other block." Because of this, the function can be simplified slightly:

function checkObj(checkprop) {
if (myObj.hasOwnProperty(checkprop)) {
return myObj[checkprop];
} else {
return 'Not found';
}
}

The == true is, like the previous example, just there to make it obvious - because hasOwnProperty returns true or false anyway, there is not point checking if true equals true: it does.