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 Introduction to Programming Control Structures If/Else

Nejc Vukovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,271 Points

Question 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

Erik Krieg
Erik Krieg
43,037 Points

Including 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,271 Points

Hi 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.

Erik Krieg
Erik Krieg
43,037 Points

Nice, in the end it comes down to personal preference or if you work with a team of devs, the team's preference .

Nejc Vukovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Nejc Vukovic
Full Stack JavaScript Techdegree Student 36,271 Points

True. 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:)