tags:

views:

183

answers:

10

I hope I won't get downvotes in this post.

But I can't figure out why Javascript needs you to insert semicolon at the end.

Can't it figure out when the line ends?

And will Javascript ever get rid of semicolon you think?

+6  A: 

Take a look at minified JavaScript and then try and imagine it without the semicolon; being able to throw out the whitespace can have significant enough impacts on file-sizes and that can be reason enough.

Less specifically it's very much a give-and-take. Firstly, in programming there is a certain necessary amount of punctuation. Try reading a book and ignoring the periods and capital letters, and see how much the meaning can become ambiguous as the sentences fall apart. The same is true in programming; without the punctuation you can no longer clearly interpret what is being said.

The give and take comes in terms of how you represent those end-of-statements. Some languages such as Python use stricter white-space conventions to denote where statements begin and end, leading to code which is very easy to read. Other languages are much more strict than JS in terms of syntax requirements--which can become quite hard to read, but is also very explicit.

STW
When you minify javascript then the semicolons could be outputted. But it would look nicer without them in my original source code. +1 though for not having to convert anything between minification and original source code.
never_had_a_name
You could always write your own language and a compiler to emit valid JS. Of course your language would look very much like JS, just with the syntactical sugar. Take a look at lesscss.org for an example (it's a smarter CSS which compiles to regular CSS)
STW
+1 I love the semicolon use when talking about punctuation; a very nice touch
Darko Z
python doesn't use whitespace to replace semi-colons, it uses whitespace to replace squiggly brackets. not 100% sure (really not a python guy) but most languages like it only require semi colons if you are jamming more then one statement on the same line, otherwise it is implied with a newline. IMO this is intelligent, as you are optimizing for the 99.99999% case (one statement per line) rather then adding an extra character unnecessarily.
Matt Briggs
your minifier example doesn't really make your point. A newline doesn't take any more space than a semicolon, so a minifier could use a newline anywhere it uses a semicolon. Its removing the indentation that reduces file sizes.
Jeremy Wall
Unless the script uses CRLF for line endings, which is twice the size of a semicolon, yes?
harto
@Matt -- I could have better worded it, but my point was that Python uses whitespace and syntax conventions in place of explicit symbols
STW
@Jeremy -- correct, the actual new-line (even if it's CRLF) doesn't provide much space saving--but there almost always whitespace after the new line which is wasted bytes
STW
+1  A: 

Personally, I quite like having line-end statements. It helps me to read the code and, if I'm feeling quirky, lets me put more than one statement on the same line. It also allows for compression, and avoids the quirks presented by different OSes using different end-of-line/new-line characters.

So, in short, I really hope not (unless it's to replace it with another end-of-line/end-of-statement character, but that would just be pointless).

David Thomas
There is already an end-of-line marker, it's \n. Writing code to handle differing newline convention characters is trivial compared to writing a javascript parser. Semicolons are redundant except when putting more than one statement in a single line.
Lie Ryan
+3  A: 

Personally, languages that don't need an EOL character annoy me.

Formatting my code, let's say, a SQL statement is so much better:

$SQL = "SELECT
              *
        FROM
              table";

Vs

SQL = "SELECT * FROM table"

Sure, it doesn't matter much for this SQL statement, but when you start getting complex ones...

SQL = "SELECT customers.* FROM customers LEFT JOIN orders ON (customers.customer_id =                 orders.customer_id AND year(orders.order_date) = 2004) WHERE orders.order_id IS NULL"


$SQL = "SELECT 
              customers.* 
        FROM 
              customers
        LEFT JOIN 
              orders
        ON (
            customers.customer_id = orders.customer_id 
            AND year(orders.order_date) = 2004
           )
       WHERE
            orders.order_id IS NULL

Which is more readable to you?

Chris Sobolewski
+1 - To me, semicolon in SQL improves readability.
Tim
That problem could also be solved with a multiline string syntax, which is probably the thing I miss the most when working heavily with javascript.
Matt Briggs
+1  A: 

Semicolon is a formally necessary part of java script statements:

Practically speaking, it is not needed in most cases. The browser can usually parse statements based on new-lines, line-breaks and other informal conventions.

It is a good idea to put it anyways, though, because for example, a person may have the option to use strict JS in Mozilla turned on, and they might get syntax warnings in the console without it.

Also, in certain cases (e.g., in a bookmark let) the lack of the semicolon may cause the evaluation of the statement to fail because there is no new-line or space or line-break between two statements (e.g., var a="Henry the ",b=7;++b;alert(a+b+"th\nI am, I am...");).

Wazzy
A: 

Also, Javascript takes its syntax from C. It wouldn't make sense to just take away semi colons for JS.

MT
A: 

It's like the period at the end of a sentence. You don't need it per se, but it increases readability and allows multiple statements to (judiciously) be placed on one line.

It also increases the possibilities for minification by providing a concrete separator.

It is a deterministic statement separator; there is no doubt about its meaning or use.

Lastly, removing the requirement for a semicolon would also make Javascript inconsistent with most other C-style languages which--like it or not--comprise a huge codebase that won't be changing any time soon.

Tim
what about all the languages where semi colons are only needed if you need to terminate a statement before the end of the line? it would drive me insane if I had to have a semi colon after every shell command for example. Why is it needed in source code?
Matt Briggs
@Matt - I can only comment from having done years of Basic/VB, SQL which rely less on punctuation and years of JavaScript, C++, Java, and C# that are punctuation-heavy. To me, the punctuation always wins for readability in a program of any length. It's a visual cue that quickly infers structure (the same as curly braces do, but that's a different argument). Everyone is entitled to whatever works best for them, but I wouldn't expect the JS standard to change, which was the OP's question.
Tim
@Tim: I don't expect it to change either :-) VB and SQL are great examples, because IMO VB has a terrible way of handling this (underscore for _any_ line break), and SQL does it the (imo) right way (semi colon only if you run two queries on the same line). Out of curiosity, what kind of situations do you git bitten by the optional nature of semi-colons in SQL?
Matt Briggs
@Matt - with regards to SQL, I haven't found many subtle bugs caused by omitting semicolons, because they are indeed optional. However, it does improve the readability of SQL (IMO) because it clearly shows when a statement is complete, rather than the many times a SQL statement continues into multiple lines. You could argue that optional semicolons would have the same effect in JS; I would argue that it's a consistency thing. Because I format my SQL and JS with semicolons, I now expect to see them in my code on a consistent basis.
Tim
@Matt - Lastly, language/semantics doesn't make for a better coder, but I've noticed that poor coders gravitate towards the languages that they can make the most mistakes/inconsistencies and still "get away" with it. If don't care if you prefer a language that requires all caps or no semicolons or writing in 3 character instructions, as long as it is structured and consistent to you and other people reading your code.
Tim
@Tim: I get where you are coming from with the first point, if you have semi-colons everywhere you sort of get used to them, so if only one language out of many used to develop an app doesn't use them, that can be sort of jarring. The second though I think is a correlation vs causation thing, some of the greatest programmers also gravitate towards those languages. LISP for example, which probably has as few rules as is possible, is also consistently the language that the great coders get misty eyed over.
Matt Briggs
+2  A: 

The ; is not a EOL convention. It depicts the end of a process request.

DO THIS; DO THAT; DO MORE;

By convention people have become accustomed to placing it on a single line, but it's completely unnecessary. You can have a single process request span multiple lines or multiple processes on a single line. The only way to know when it ends is by the ; declaration.

Therefore, it is ABSOLUTELY necessary to declare the end of the process. In Javascript you can get away with not using the ; (in some cases) when it is immediately followed by other process closures such as the brace (}).

It also stems from older languages such a C. So your question should be why do programming languages use characters such as ; to declare the end of a process? (see my answer above and others as well)

nopuck4you
+1  A: 

You need it because of IE. You can end up with bizzarre and subtle bugs without it, and the rules for when you actually need it or not are rather arcane, so it is a best practice to use it all the time.

That being said, adding a keystroke at the end of every line for no real reason is the very essence of useless noise in code, I would recommend checking out the excellent coffeescript, which eliminates most of the silly things from javascript syntax.

Matt Briggs
I don't know why this post is downvoted. While I don't agree, everyone is free to have an opinion. JavaScript is an incredibly flexible language, but it certainly has its unintuitive moments. What makes a language intuitive is a matter of opinion. +1 for the coffeescript link...it's an interesting idea.
Tim
@Tim: The downvotes are because everyone who answered this are c/c++/java/c# people, I am a perl/ruby/lisp kind of guy, and was a little inflammatory in how I described my opinion, so it probably stepped on a few toes :) IMO javascript is an amazing language with an awefully verbose syntax, and coffeescript fixes that for me. But to each his own
Matt Briggs
A: 

Actually, JavaScript does mostly work if you leave out all the semicolons. The browser has to try to figure out where your statements end, which it does according to a standard set of rules. It's called automatic semicolon insertion.

However, it's a terrible idea to rely on this. The rules are complicated and they might not always do what you expect. For example, what does this program mean?

f(x)
(new Foo).start()

It turns out that JS does not treat this as two separate statements. This is the same as:

f(x)(new Foo).start();

which... is probably total nonsense, but actually there's a JS library, (fab), that depends on JS treating this as a single statement.

A major problem with leaving off the semicolons is that if you make a mistake, sometimes JavaScript accepts your program, but interprets it differently from the way you intended.

(Also: every time a new bit of syntax is proposed for the ECMAScript standard, the committee has to worry about whether it changes the meaning of existing JS code with missing semicolons! It's a drag.)

To answer your original question, "Will Javascript ever get rid of semicolon at the end?": no. That would, of course, make your browser reject every existing scrap of JS code on the Web. The existing rules are very well established and necessary for compatibility with the Web as it is.

Jason Orendorff
+1  A: 

In most cases, semicolons are unnecessary in Javascript. The interpreter can usually makes sense of things without them. But there are times when they are needed to resolve ambiguity. For this reason, the Google style guide for Javascript (for example) recommends always using them.

Because of these ambiguities, it's doubtful that Javascript will ever get rid of semicolons. The syntax would have to change significantly and too much old code would break.

I. J. Kennedy
But having to use something not as efficient just because it would break something old is not a good argument. Maybe we should just let it break and the coders recode the changes.
never_had_a_name