Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Functions Create Reusable Code with Functions Using Multiple return Statements

6:20 - Why only display the alert?

I'm having a hard time understanding why it wouldn't first return 5, then display the alert instead of only displaying the alert. When the program runs, why does it skip the function instead of running everything from the top down?

Tim Oltman
Tim Oltman
7,730 Points

Hi Ryan,

The noAlert() function is returning the number 5, but we just aren't doing anything with it. Then we are calling the alert() outside of the function. If you want to see that 5 was indeed returned from the function you can try this:

console.log(noAlert());
alert("This will appear");

Now you will see '5' logged to the console before the alert.

2 Answers

Steven Parker
Steven Parker
229,732 Points

When you enter more than one statement in the console at a time, the console only shows you the results of the last one. So when the "noAlert" function is called and an "alert" is called in the same input, the 5 does get returned but not shown.

When the pop-up is closed, you see "undefined" displayed on the console (for a moment), because the "alert" doesn't return a value.

There it is, thanks!

Michael Rushton
Michael Rushton
7,126 Points

Man, this is a pretty big thing to just slip in as a last little word on a video. So the console will only ever show what is returned from the last statement you entered if you have entered more than one? And then the reason it says "undefined" right at the end is because the alert isn't returning a value? Can an alert ever return a value? Ahh man that's confusing as Hell 😂😂. I can just imagine looking at the console and wondering why the value i expected wasn't showing up for hours on end not knowing this. So why does it display the 5 the first time if we aren't doing anything with that either?