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 Selecting Elements and Adding Events with JavaScript Perform: Selecting Elements

Francesco Belvedere
Francesco Belvedere
15,206 Points

Shouldnt there be semicolons at the end of each var statement when declaring a function this way?

I was under the impression that when a variable is declared and set that it always gets a semicolon to end the statement, for example:

var color = "red" ;

Isnt that essentially the same as this anonymous function below? var myFunction = function() {};

Isnt the semicolon NECESSARY above in this anonymous function statement?

As opposed to the other way of writing a named function that DOES NOT need a semicolon like this?

function name() { code in here }

Given that JS is so picky and that missing semicolons can introduce bugs I would love the final word on this.

When are they needed and when are they not needed?

Thanks FB //

8 Answers

When are they needed and when are they not needed?

I find it's best to use semi-colons as much as I can. Especially if the code I am writing is eventually going to be minified.

I found this while researching when its optional to add semi-colons: http://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript

It's exactly what I would have written :)

Francesco Belvedere
Francesco Belvedere
15,206 Points

Right ok.

So since the first function is essentially being assigned to a variable STATEMENT the semicolon is required.

Whereas the second named function in my example shouldnt have it following the closing curly brace.

Thanks! Great reference.

Exactly :)

Francesco Belvedere
Francesco Belvedere
15,206 Points

In the video Chalkley assigns five functions anonymously to a variable but OMITS the final semicolon after the curly brace.

In my code when following him I added the final semicolons since they are statements and my code is working fine so far.

I just wanted to be sure. I think these small standards details need to be adhered to especially in courses like this.

Chalkley is a great teacher and has helped me immensely with my understanding of JS. But these small details can stop me in my tracks sometimes as I struggle to understand the syntax and rules.

FB //

It happens. I recommend checking out JSHint or ESLint for catching syntax stuff. :) Depending on your editor you can include it as a plugin or just run it from the command line.

Francesco Belvedere
Francesco Belvedere
15,206 Points

Yes cool. I have heard of JSLint.

I am still in the foundations of JS but will keep that in mind when i get around to authoring more stuff.

Thanks again.

FB //

Olga Kireeva
Olga Kireeva
9,609 Points

Hi Francesco, I'd like to try to answer your question. Some programming languages (JavaScript is among them ) use a semicolon to signify the end of a statement. It's probably done to avoid situations when two or more statements can be read as a single one.
JavaScript statements are separated by a semicolon. However,semicolon isn't used after function declarations . And here you need to make a difference between function declarations and a variable assignment (when you assigning a function to a variable). For example, var myVar = function() { alert("It is red!"); }; // this is an assignment statement

function foo() { alert("It is red!"); } //no semicolon because it' just a function declaration and this function hasn't been called yet

You also need to use semicolon if a function is used in return statement or with a self-invoking function.

JavaScript is incredibly lenient about semicolons and most of the time, semi-colons are unnecessary, so long as commands go to separate lines. You should still write your code with semicolons because as Erik mentioned, if it's going to be minified, it can cause issues due to long lines of code being on the same line.

Here are some valid pieces of code not utilizing semicolons i.e.

//Perfectly valid and executable self invoking function
//that utilizes no semicolons
(function () {
return true
//also valid and executable
var a = 3 
//This is invalid and will throw an error
//notice two initializations on one line
var b = 2 var c = 4
//This however is valid, no semicolon on end
var d = 5; var e = 6
//Also valid, variable declaration, no semicolon on end
x = 2; y  = 10
//Another valid piece of code
var myfunc = function () {

//for statements always require semicolons to separate each part of the statement
for (var i=0; i<10; i++) {
Francesco Belvedere
Francesco Belvedere
15,206 Points

Marcus //

Just because a language is lenient doesnt mean we should be lazy with our code does it?

From what I have learned about JS so far is that it can be hard to debug and semicolons can be a big culprit.

What I am looking for here is best practices and a sort of standard or a style that is used MOST of the time.

Erik's answer above was very clear and pretty much summed it up and provided a style guide that I think I will stick to.

FB //

A quote from my comment: "You should still write your code with semicolons...". I was addressing what Olga said that you need to use a semicolon. I write all of my code with semicolons, and I have done some very big projects: http://www.marcusparsons.com/projects/sketchmeh, http://www.marcusparsons.com/projects/weatherapp, http://www.marcusparsons.com/projects/calculator, and http://www.marcusparsons.com/projects/todolist/index.html. No one said anyone should be lazy.

Francesco Belvedere
Francesco Belvedere
15,206 Points

Thanks for clarifying that Marcus.

I didnt meant to challenge your skillset and/or proficiency with the language, nor to imply that you are saying anyone should be lazy.

Just looking for solid, straight forward answers and best practices.

JavaScript has been extremely challenging for me and looking for benchmarks.

FB //

If you're intent on learning JavaScript, then I would argue that you should know what is valid and invalid, as well, as that will help with debugging in the future.

Francesco Belvedere
Francesco Belvedere
15,206 Points

Of course — good to keep in mind.

Thanks again for your help.

FB //

Absolutely. Good luck to you, Francesco!