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 trialRyan Soeder
11,056 Points6: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?
2 Answers
Steven Parker
231,248 PointsWhen 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.
Ryan Soeder
11,056 PointsThere it is, thanks!
Michael Rushton
7,126 PointsMan, 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?
Tim Oltman
7,730 PointsTim Oltman
7,730 PointsHi 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:
Now you will see '5' logged to the console before the alert.