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 trialNejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsQuestion about variables
Hi there.
Isn't it safer to end every statement and variable declaration with a ";" ?
As I watched the video and Jim didn't end the var declaration with a ";" I was surprised that the variable was set to the value he declared and didn't throw an error that it's missing.
Could somebody share some light on this?
I know he stated at the beginning of this course that you don't need to end single line commands but he does advise it.
Isn't it better to end it with a ";" ? mean I've been doing it like this from the starts and I'm just surprised that no error was thrown.
Thanks for the explanation.
3 Answers
eck
43,038 PointsIncluding the semicolons at the ends of your lines of code can help reduce the risk of unintended behaviour if that code is compressed. It can also improve readability and promote good habits for coding in other languages, like PHP, which are much more strict about the usage of semicolons.
You can read some more here about semicolons in JavaScript.
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsHi Erik,
Thanks for the heads up.
I mean I'm used to C++, PHP and I did some JavaScript but I never noticed that the semicolons are not a must in JS...
That's why I was a bit surprised. Thank you for your answer and I'll check the link you provided.
eck
43,038 PointsNice, in the end it comes down to personal preference or if you work with a team of devs, the team's preference .
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsTrue. I think it also defaults to with which programming language you start.
C++ a semicolon maniac.
Python "Whats an semicolon" ;)
But I enjoy learning them all:)