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 Functions Arrow Functions Concise Arrow Function Syntax

Shouldn't the final code example on this stage end with a semicolon (...compared to the following function:)?

The last example of code on this page is a statement:

const greeting = function() {
  alert(`Greetings, ${name}!`);
}

So shouldn't it end with a semicolon like this?

const greeting = function() {
  alert(`Greetings, ${name}!`);
};

function declarations don't need to have semicolons in the end in JavaScript. In fact, in javascript you can omit most semicolons. I don't add semicolons at all when I write JavaScript at work. https://flaviocopes.com/javascript-automatic-semicolon-insertion/

@ Ella Ruokokoski

I just find it weird how it's left out there but Guil says it's needed just 3 steps earlier at 0:57 in this step:

https://teamtreehouse.com/library/function-declarations-vs-function-expressions

3 Answers

As the other two said earlier you don't technically need to have semicolons, however, most of the time people add them as a standard practice. It is not necessary and JavaScript won't throw an error if it isn't there, however, most of the time the standard practice is to add semicolons at the end of lines and statements.

Hi Matthew,

Yes. You should use a semicolon here because that is an anonymous function. There was a previous lesson that described it as "storing a function in a variable" and "creating a statement by assigning a value to a variable". Statements do require semicolons at the end so the closing brace should have a semicolon after it.

That said, we may just be seeing the "proper" way of coding JavaScript. In practice, it is not necessarily required because of the JavaScript engine, just as Ella's article points out. I tested it both ways in my browser and there were no errors when I didn't use the semicolon.

Keep in mind, there may be more advanced code that we haven't been taught yet that will yield different results. Perhaps when you create more advanced code and you don't have the proper syntax, the JavaScript engine may not catch it. Better to code properly vs. potential problems down the road?

Cheers!

J A
seal-mask
.a{fill-rule:evenodd;}techdegree
J A
Full Stack JavaScript Techdegree Student 4,646 Points

As others have stated you can omit semicolons in many cases when writing simple JavaScript code. That being said, just because you can, does not mean you should. I consider it a best practice to end all your expressions with a semicolon. Always leaving out semicolons when writing code can lead to subtle bugs (see this StackOverflow Answer for an example).