Reputation: 23
userMsg = toUser("Bob") is calling toUser("Bob") and assigning it to userMsg which is then being concatenated with name and assigned to greet. So my brain is wanting to see greet = toUser("Bob") + name which is obviously not javascript is doing. What is going on with userMsg = toUser("Bob"); Is userMsg = message? or is userMsg = toUser("Bob");
Please help me to understand. Why is this...
function giveMessage(message) {
let userMsg = message;
userMsg = toUser("Bob");
return userMsg;
function toUser(userName) {
let name = userName;
let greet = userMsg + name;
return greet;
}
}
console.log(giveMessage("The world says hello dear: "));
// The world says hello dear: Bob
the same as this?
function giveMessage(message) {
let userMsg = message;
let hello = toUser("Bob");
return hello;
function toUser(userName) {
let name = userName;
let greet = userMsg + name;
return greet;
}
}
console.log(giveMessage("The world says hello dear: "));
// The world says hello dear: Bob
Upvotes: 2
Views: 64
Reputation: 12978
Here is your first code snippet annotated with line numbers:
function giveMessage(message) { // line 1
// line 2
let userMsg = message; // line 3
userMsg = toUser("Bob"); // line 4
return userMsg; // line 5
function toUser(userName) { // line 6
let name = userName; // line 7
let greet = userMsg + name; // line 8
return greet; // line 9
} // line 10
// line 11
} // line 12
console.log(giveMessage("The world says hello dear: ")); // line 13
The function giveMessage
is defined starting on line 1, but is only invoked when it is called inside console.log()
on line 13.
When it is invoked, the order of execution steps will be:
userMsg
to the value of message
which is "The world says hello dear: "
)toUser("Bob")
.name
to be the passed in value of userName
which is "Bob"
.greet
to the concatenated value of userMsg
and name
. Note that userMsg
still holds the value that was assigned in step 1, since we are still only evaluating the right hand side of line 4. This means that greet
gets set to "The world says hello dear: " + "Bob"
.greet
, and we jump back to line 4 to complete the assignment we began in step 2. Now we are setting userMsg
and overwriting the value previously set in it, but that value was already used back in step 4, so we haven't lost anything.userMsg
.Your second code snippet simply introduces a new variable to hold the output of the call to toUser(...)
, and will have no effect on what happens inside toUser(...)
.
Upvotes: 2
Reputation: 19957
@Ergwun has already posted a good answer. I'd like to add just another angle to view the problem. You can simply "inline" the function to get an equivalent by replacing return
with userMsg =
. Hope this helps to clarify your doubt.
function giveMessage(message) {
let userMsg = message;
// userMsg = toUser("Bob");
// "inline" the `toUser` function:
{
let userName = "Bob"; // toUser(userName = "Bob")
let name = userName;
let greet = userMsg + name;
userMsg = greet; // return greet;
}
return userMsg;
}
console.log(giveMessage("The world says hello dear: "));
Upvotes: 0