# Question about function inside of function

**URL:** <https://forum.kirupa.com/t/question-about-function-inside-of-function/636761>\
**Category:** programming\
**Created:** [June 13, 2017, 8:29am UTC](https://forum.kirupa.com/t/question-about-function-inside-of-function/636761 "2017-06-13T08:29:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamgeorgegreene](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@iamgeorgegreene](https://forum.kirupa.com/u/iamgeorgegreene)\
**Post date:** [June 13, 2017, 8:29am UTC](https://forum.kirupa.com/t/question-about-function-inside-of-function/636761/1 "2017-06-13T08:29:07Z")

</div>

Hello,

I am reading Javascript for Absolute Beginners and I have a question about the following referenced code…

```auto
function youSayGoodbye() {
 
    alert("Goodbye!");
 
    function andISayHello() {
        alert("Hello!");
    }
 
    return andISayHello;
}

```

When I call the function youSayGoodbye() at [https://jsfiddle.net/](https://jsfiddle.net/) I see one alert that says “Goodbye!”

Then when I initialize a variable:

```auto
var something = youSayGoodbye();

```

and call:

```auto
something()

```

I get two alerts 1st: Goodbye! and 2nd: Hello!

My questions are:

1. The book says:  
The youSayGoodbye outer function, from the something variable’s point of view, simply goes away. Because the something variable now points to a function, you can invoke this function by just calling it using the open and close parentheses like you normally would. When you do this, the returned inner function (aka andISayHello) will execute. Just like before, you will see a dialog box appear, but this dialog box will say Hello!—which is what the alert inside this function specified.

My issue is understanding why when I use something () at [https://jsfiddle.net/](https://jsfiddle.net/) (One for Hello! and One for Goodbye!) When the wording from the book makes seem it like I am just supposed to get one (Hello!).

I am confused. My apologies for so much text… please explain (wipes sweat beads from head caused by noob programming anxiety. ) Thanks!

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 13, 2017, 4:23pm UTC](https://forum.kirupa.com/t/question-about-function-inside-of-function/636761/2 "2017-06-13T16:23:50Z")

</div>

I think you forgot to save your fiddle, so your jsfiddle links go to a blank document 😉 But assuming you didn’t change anything from the book (I broke mine out to make sure there wasn’t anything much more going on other than the code you posted), it goes like this.

First, what’s getting discussed is:

```auto
function youSayGoodbye() {
 
    alert("Goodbye!");
 
    function andISayHello() {
        alert("Hello!");
    }
 
    return andISayHello;
}
var something = youSayGoodbye();

```

With this code alone you should _only_ see “Goodbye!”. The `youSayGoodbye` function runs, calls the `alert`, and _defines_ but does not _run_ `andISayHello` which is then returned. That function definition then gets reassigned to `something`. The text before what you quoted in the book covers this, mentioning that “Good Bye!” is alerted (Doh! Typo!).

After that, the `something` function gets called.

```auto
something();

```

And from that invocation, “Hello!” is _additionally_ alerted. Focusing on just `something()` we say that only “Hello!” is alerted, with the understanding that “Goodbye!” has already happened.

I think what’s confusing that, for context, the code in the book showing `something()` includes the assignment above it:

```auto
var something = youSayGoodbye();
something();

```

So in this case, yes, both alerts will appear as a result of running this code, but when it sounds like it should only be “Hello!”, its because the text is focusing specifically on the results of `something()` and not the assignment where `youSayGoodbye()` is called.

In the end, what you’re seeing is correct. Both alerts occur - each of which occurring as their respective function is called, wherever that happens in the code.

---

<div class="post-metadata">

**Author:** ![iamgeorgegreene](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@iamgeorgegreene](https://forum.kirupa.com/u/iamgeorgegreene)\
**Post date:** [June 16, 2017, 12:38pm UTC](https://forum.kirupa.com/t/question-about-function-inside-of-function/636761/3 "2017-06-16T12:38:43Z")

</div>

Thank you so much!
