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 Basics (Retired) Introducing JavaScript The Console Challenge Answer

Ross Horak
Ross Horak
20,890 Points

Why doesn't console.log('begin program'); on line 1 execute if there is an error after it in the program?

I added console.log('begin program'); to the first line of my javascript program. Before correcting the syntax errors further down in the program I reloaded the page and expected to see 'begin program' output in the console followed by error messages. However this is not the case. Only the error message is output in the console. Only once I fixed the errors did the console.log output work. Why is this? My thinking was that if the program executes line by line the console.log output on line 1 should output regardless of errors on subsequent lines of the program.

Related post: this only occurs when the error is in the string formatting, not if the error is in the function name:

https://teamtreehouse.com/community/error-in-function-name-allows-code-to-run-until-error-error-in-string-causes-everything-to-block-why

Ross Horak
Ross Horak
20,890 Points

Thanks for posting that answer Kenny - makes perfect sense!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

My first guess would be because of function hoisting. You say "line by line" but JavaScript doesn't always run that way. It's possible to use a function before you even declare it because of this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function