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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with children

declaring function in variable

In javascript basic course dave mcfarland said that if we declare function in variable we need to put ' ; ' after the function block statement like this. if we don't it caused error.

var foo = function() {
};

But Andrew not using any semicolon in his course after declaring the the function in variable and caused no error.

var foo = function() {

}

3 Answers

It is a best practice to include semicolons, but it is not required for your code to run correctly. You can read a bit about it here

JS will automatically add semicolons if you leave them out. There can be problems though; the semicolons are not always added in the spot you had intended. This can result in bugs or other undesired behavior. So it is best to be explicit and place those semicolons yourself :)

Sean T. Unwin
Sean T. Unwin
28,690 Points

Semantically a semi-colon should be used after a variable declaration, although most JavaScript interpreters will not fail if one is not used.

thnx now i got the answer :)