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 Foundations Functions Anonymous Functions

Jiecao Wang
Jiecao Wang
5,833 Points

Line 72 ";" needed?

why don't we have ";" right after the execution "()" on line 72 for the very last example in the video?

1 Answer

Jose Morales-Mendizabal
Jose Morales-Mendizabal
19,175 Points

Perhaps the reason Jim did not include a semi-colon at the end of the function statement is because it was the LAST statement in the source code. During runtime, the JavaScript interpreter distinguishes one statement from another when it encounters a semicolon as it executes from top to bottom. Essentially the semicolon tells the computer, "Ok, move on to the next instruction". In this case since the anonymous function was the last statement, the computer executed the function and stopped executing the source code since it had reached the end. Had Jim included another snippet of code below that function, say a sum of two numbers, there would have been an error.

To Illustrate this idea look at the following code:

console.log("This statement requires a semicolon at the end to tell the javascript interpreter to move on to the command below");
console.log("This statement does not, and will not produce any errors")

I would not advise doing this in a real-world production environment, however, since this scenario could cause bugs in the future as a codebase grows. So yeah, by not including a semi-colon Jim deviated from a coding best practice. Always remember to put a semi colon at the end of every statement! Even if it's the last one :)

Jiecao Wang
Jiecao Wang
5,833 Points

Very thorough, having a really busy week, didn't get a chance to look over treehouse