+ 5

Help me understand the return statement in the nested function.

function showName(firstName,lastName) { var nameIntro = "This is "; function displayName() { return nameIntro + firstName + " " + lastName; } return displayName(); }

21st Feb 2017, 6:19 AM
kimwele
kimwele - avatar
4 odpowiedzi
+ 4
what if I just remove the return displayName() and use displayName();
21st Feb 2017, 6:52 AM
kimwele
kimwele - avatar
+ 4
Thanks, that was helpful..
21st Feb 2017, 7:09 AM
kimwele
kimwele - avatar
+ 2
Here, the inner function displayName is defined within the function showName, but it is not called until you get to the return displayName(); statement. At which point the inner function returns (nameIntro + firstName + " " + lastName;) to the outer functions return statement which will then return to where the outer function showName was called.
21st Feb 2017, 6:43 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
function showName(firstName, lastName) { return "This is " + firstName + " " + lastName; } This is essentially the same thing. Provided there isn't a call somewhere else to the inner function itself. Similar to: showName(firstName, lastName).displayName();
21st Feb 2017, 7:04 AM
ChaoticDawg
ChaoticDawg - avatar