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 trialErin Clayton
3,840 PointsWhy doesn't prompt() need a semi-colon to work?
So what are the semi-colons really there for?
4 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsPrompt() is function or method declaration which doesn't require semicolons. I think you may need one depending on how you use it. If you assign it to a variable you may have to end the line with a semicolon. Semi colons help end statements but methods aren't necessarily statements. It's more like an expression like an if
statement.
However you can safely use them if you prefer. The Semi colons don't terminate lines of code, merely help separate them from the other! :-)
John Fry
3,138 PointsYou don't need to add the semi-colon to the last statement in a script. But its just a good idea to do it so you don't forget elsewhere. As soon as he added the console.log after it then he had to add the semi-colon.
Nourez Rawji
3,303 PointsThe semicolon is a way to separate statements. Technically, you don't need it on a single line program, or on the last line of a file (as there aren't any statements to separate at that point), but it's good practice to always use it at the end of a line. After all, you could add more code later and a missing semicolon there could result in a syntax error.
William Whatley
5,452 PointsTechnically you are not required to include semicolons as line ends anywhere in JavaScript-- it's a great practice though because many other programming languages due require semicolons
Simon Coates
28,694 PointsSimon Coates
28,694 PointsNot super sure about javascript, but some languages are okay with places where they can work out that there should be a semicolon. Even in instances where code is forgiving, it may be best practice to include anyway. The semicolon in most languages indicates the end of an instruction. Looked at stackoverflow and saw an answer: 'Javascript does something called "semicolon insertion" which means you can actually write code that omits the semicolon in certain places, and they'll basically be added for you when the code is parsed.
The rules around when this happens a little complex. For simplicity's sake, many developers simply pretend semicolon insertion doesn't exist.'