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
Unsubscribed User
8,841 PointsQuestion on assigning a function to a variable
can you let me know the significance of the semicolon ';' when assigning a function to a variable.
I executed the code in all the above scenario and checked that it is running successfully.
Scenario 1:
var myFunction = function (a, b) {
var c = a +b;
return c;
};
Scenario 2: without ';' at the end of function
var myFunction = function (a, b) {
var c = a +b;
return c;
}
scenario 3: without ';' at all sentences of the program.
var myFunction = function (a, b) {
var c = a +b
return c
}
1 Answer
Jason Anders
Treehouse Moderator 145,863 PointsWith a few exceptions, semicolons are optional in JavaScript, as they are 'statement separators' and not 'statement enders.'
If your code is on one line, you do need semicolons (eg `var i = 0; i > variable; ....), but if the code is separated by a line break, you don't need the semicolon.
I use them. One, because I think it makes the code easier to read (one reason I'm not such a Ruby fan). Two, if by some chance you minify your code and it ends up on one line, it will break.
Stackoverflow has arguments for both sides... give it a read and go from there.
Keep Coding! :)
Unsubscribed User
8,841 PointsUnsubscribed User
8,841 PointsThank you for the explanation. This gives me a good knowledge on semicolon :)