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 JavaScript Basics (Retired) Storing and Tracking Information with Variables Capturing Visitor Input

Erin Clayton
Erin Clayton
3,840 Points

Why doesn't prompt() need a semi-colon to work?

So what are the semi-colons really there for?

Simon Coates
Simon Coates
28,694 Points

Not 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.'

4 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Prompt() 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
John Fry
3,138 Points

You 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
Nourez Rawji
3,303 Points

The 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
William Whatley
5,452 Points

Technically 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