Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Y B
Y B
14,136 Points

Ending a function declaration with ;

I keep on ending functions I setup with ; which I understand isn't needed (only if assigning to a variable at the same time)

e.g. function foo () { };

Does this cause any issues that aren't obvious?

2 Answers

Colin's got a good argument for including them, but I'd like to share this comment I just read on a StackOverflow thread about this, which added to my own understanding of how this works:

"Some of the confusion here may be influenced by lack of a good English word for "permitted because it will be ignored". We fall back on saying "optional", but that's misleading since it suggests that not including a semicolon after a declaration falls in the same category as not including a semicolon after a statement. The latter is optional in an entirely different sense: there it is because the parser will add the missing semicolon you omitted, while in this case, it is because the parser will ignore the semicolon you included."

Rich Donnellan
Rich Donnellan
25,780 Points

Cena,

The > (Markdown for block quote) might be a better choice, as your quote is very difficult to read (gray on black with horizontal scroll).

Yeah, fixed as soon as I posted. :)

Y B
Y B
14,136 Points

Thanks that link was perfect

Colin Marshall
Colin Marshall
32,861 Points

Good answer, that makes perfect sense.

Colin Marshall
Colin Marshall
32,861 Points

It is optional, but I always put the semicolon in there. If you run your scripts through a minifier to reduce file size, your code can become broken if you are omitting semicolons because it places all of the code on one line and the interpreter will get confused without the semicolons.

Rich Donnellan
Rich Donnellan
25,780 Points

For functions? Nope; it's unnecessary.

Colin Marshall
Colin Marshall
32,861 Points

You're right you don't need them on functions no matter what. And you shouldn't have them. I was speaking in general on semicolons because there are situations where minified code will not run if they are omitted.

Rich Donnellan
Rich Donnellan
25,780 Points

Agree with you there, Colin. It wasn't entirely clear and I'd hate for others to misinterpret your answer.

Y B
Y B
14,136 Points

Thanks I'm not talking about ending statements with a semicolon, but about the specific case of ending a function with them. It still seems to work if I include them, but I'm not clear if this causes an issue - is what does a semicolon at the end of a function declaration mean to the interpreter?