zachary colten
zachary colten

Reputation: 23

What is happening step by step in this code to get the commented output?

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

Answers (2)

Ergwun
Ergwun

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:

  1. Execute line 3 (sets userMsg to the value of message which is "The world says hello dear: ")
  2. Begin executing line 4, by evaluating the right hand side of the assignment expression (i.e. the call of function toUser("Bob").
  3. Execute line 7 (as part of the function call), and asign name to be the passed in value of userName which is "Bob".
  4. Execute line 8, which sets 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".
  5. The function returns he value of 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.
  6. Return the new value of 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

hackape
hackape

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

Related Questions